1066C - Books Queries I'm trying to solve this problem using deque, but get WA, can any one tell me why it is wrong 155581257
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
1066C - Books Queries I'm trying to solve this problem using deque, but get WA, can any one tell me why it is wrong 155581257
Name |
---|
== But, unlike vectors, deques are not guaranteed to store all its elements in contiguous storage locations: accessing elements in a deque by offsetting a pointer to another element causes undefined behavior. == I think, you can't use difference of addresses.
Difference of addresses of deque's items surely not to be used. But in code used only differences of iterators, which is correct.
Problem is there: http://open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4861.pdf#subsubsection.22.3.8.4
An insertion at either end of the deque invalidates all the iterators to the deque
. So using stored iterators after push_back/pop_back is UB.Difference of iterators is difference of addresses, as i understand. On non-linear containers metod 'distance()' need to be used to get count of elements between two iterators.
Invalidation will cause troubles as well. So, method 'distance ()' doesn't help to solve the problem.
Nope, difference of iterators is literally answer to question "how many times I need to decrement".
operator-
must be correct for allRandomAccessIterators
. Deque providesRandomAccessIterator
. It not just pointer but more complex struct.Ok, agree. Thank you.
Thank you.