Qwop's blog

By Qwop, history, 5 weeks ago, In English

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?

 
 
 
 
  • Vote: I like it
  • +2
  • Vote: I do not like it

»
5 weeks ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

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.

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it +9 Vote: I do not like it

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

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks a lot for sharing.

    • »
      »
      »
      5 weeks ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      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.

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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.

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    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.

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

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

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it +6 Vote: I do not like it

        Well you can use a struct but for me it's easier to just use std::array instead.

»
5 weeks ago, # |
  Vote: I like it +10 Vote: I do not like it

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';
»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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

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