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

Автор positivedelta2211, 3 года назад, По-английски

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

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

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

== 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.

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

    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.

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

      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.

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

        Nope, difference of iterators is literally answer to question "how many times I need to decrement". operator- must be correct for all RandomAccessIterators. Deque provides RandomAccessIterator. It not just pointer but more complex struct.

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

      Thank you.