Блог пользователя Qwop

Автор Qwop, история, 4 года назад, По-английски

Are there any pros of using std::array<type, 2> over std::pair<type, type>, other than the most obvious one that std::pair supports different types while std::array does not?

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
4 года назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

To be honest, I have never even found any problem where using std::array will be better than vector/array.

Do you know any such problem?

Edit: Why the downvotes?Lol. It's makes no sense. I just asked for a problem where I can use std::array.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +9 Проголосовать: не нравится

    vectors are indeed slower then std::array.
    TLE with vector: 1420C2.
    AC with array: 1420C2.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Thanks a lot for sharing.

    • »
      »
      »
      3 года назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      For what it's worth, most of the time overhead when using std::vector is easily eliminated by performing merge-updates in-place rather than creating and move-assigning a new vector for each one. (See 93968215.) Of course, there is still some overhead from the extra indirection in vector access, and the extra space used by many small vectors is considerable, but this will usually not be the difference between AC and TLE.

      • »
        »
        »
        »
        3 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Yeah, you are correct and i did the same thing to remove my TLE. But the point i was trying to make was that with same operations(returning a copy of vector/array) vector gave TLE and arrays got me AC.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    Segment tree problems where you store many things per node. You'll probably use a merge function so using a normal array won't work well and if you use vectors then you could get TLE.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Yeah. the link to submission i mentioned above has exact the same problem as you mentioned.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Thanks. I had not done such hard problems yet. I always used struct to make nodes where I need to store many things.

»
4 года назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

You can access std::array's elements like a normal array.

array<int, 2> a;
cout << a[0] << ' ' << a[1] << '\n'; // looks cleaner

pair<int, int> b;
cout << b.first << ' ' << b.second << '\n';
»
3 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

std::array<T, N> is just a wrapper over C-style array T[N].

https://en.cppreference.com/w/cpp/container/array