### Qwop's blog

By Qwop, history, 5 weeks ago,

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

 » 5 weeks ago, # | ← 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.
•  » » 5 weeks ago, # ^ |   +9 vectors are indeed slower then std::array. TLE with vector: 1420C2. AC with array: 1420C2.
•  » » » 5 weeks ago, # ^ |   0 Thanks a lot for sharing.
•  » » » 5 weeks ago, # ^ | ← 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.
•  » » » » 5 weeks ago, # ^ |   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.
•  » » 5 weeks ago, # ^ |   +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.
•  » » » 5 weeks ago, # ^ |   0 Yeah. the link to submission i mentioned above has exact the same problem as you mentioned.
•  » » » 5 weeks ago, # ^ |   0 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, # ^ |   +6 Well you can use a struct but for me it's easier to just use std::array instead.
 » 5 weeks ago, # |   +10 You can access std::array's elements like a normal array. array a; cout << a[0] << ' ' << a[1] << '\n'; // looks cleaner pair b; cout << b.first << ' ' << b.second << '\n'; 
 » 5 weeks ago, # | ← Rev. 2 →   0 std::array is just a wrapper over C-style array T[N].https://en.cppreference.com/w/cpp/container/array