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?
№ | Пользователь | Рейтинг |
---|---|---|
1 | jiangly | 3640 |
2 | Benq | 3593 |
3 | tourist | 3572 |
4 | orzdevinwang | 3561 |
5 | cnnfls_csy | 3539 |
6 | ecnerwala | 3534 |
7 | Radewoosh | 3532 |
8 | gyh20 | 3447 |
9 | Rebelz | 3409 |
10 | Geothermal | 3408 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | maomao90 | 173 |
2 | adamant | 164 |
3 | awoo | 161 |
4 | TheScrasse | 160 |
5 | nor | 159 |
6 | maroonrk | 156 |
7 | SecondThread | 152 |
8 | pajenegod | 146 |
9 | BledDest | 144 |
10 | Um_nik | 143 |
Название |
---|
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.
vectors are indeed slower then std::array.
TLE with vector: 1420C2.
AC with array: 1420C2.
Thanks a lot for sharing.
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.
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.
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.
Yeah. the link to submission i mentioned above has exact the same problem as you mentioned.
Thanks. I had not done such hard problems yet. I always used struct to make nodes where I need to store many things.
Well you can use a struct but for me it's easier to just use std::array instead.
You can access
std::array
's elements like a normal array.std::array<T, N>
is just a wrapper over C-style arrayT[N]
.https://en.cppreference.com/w/cpp/container/array