Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

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

Автор Dazzler, история, 7 лет назад, По-английски

Hello friends, i appeared in codeforces round 433DIV2. My solution for problem DIV2C passed the pretests but didn't pass the system test. Because there was a bug in the line 65 & 66 because i had erased the iterator and then tried to use the value stored at it(i just replaced line 65 & 66 with each other, then solution got accepted). So, it shouldn't have passed pretests even. but still it passed . Can someone explain me this abnormal behaviour of iterator.

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

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

It is happening because when you delete an element, all iterators lose their reference. Hence, operation *it after the erase line, is illegal.

source: http://www.cplusplus.com/reference/set/set/erase/

read topic : Iterator validity

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

    but it should always give RTE. then, how can it pass the pretests?

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

      "You misunderstand what "undefined behavior" means. Undefined behavior does not mean "if you do this, your program will crash or produce unexpected results." It means "if you do this, your program could crash or produce unexpected results", or do anything else, depending on your compiler, your operating system, the phase of the moon, etc." — https://stackoverflow.com/a/2874545