Dazzler's blog

By Dazzler, history, 7 years ago, In English

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.

  • Vote: I like it
  • -2
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +11 Vote: I do not like it

      "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