andpar's blog

By andpar, history, 3 years ago, In English

I am trying to solve Educational Round 115 Problem C by storing number of occurrences in the map. My submission works with MSVC https://mirror.codeforces.com/contest/1598/submission/133072945 but doesn't work with GCC https://mirror.codeforces.com/contest/1598/submission/133071532. I can't repro it locally so I can't debug it but adding check for non-existing values https://mirror.codeforces.com/contest/1598/submission/133073553 fixes the problem.

So it looks like that for missing integer values operator[] for unordered_map instead of returning 0 (default initialized value), returns uninitialized value which looks really weird. Am I missing something here?

  • Vote: I like it
  • +8
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it +22 Vote: I do not like it

If you add elements into the unordered map, then the iterator may get invalidated if rehash occurs. Thus, the order of iteration through the elements will be violated. See here for details.

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

    Thank you! I totally missed this moment