### lakshay_nasa's blog

By lakshay_nasa, 17 months ago, ,

I tried submitting a code for the problem 490B - Queue which displayed correct output for the sample case in codeblocks(ide that I use) with G++17 but it showed wrong answer on test 1(47521144), then I just changed the language to G++14 and it showed wrong answer on test 8(47521204) whereas test 8 was also working fine on my ide. Then I opened codeforces custom-test and got to see that I was getting different answers for C++17 than those of C++14 and C++11(11 and 14 were same). Then I changed my code a little bit(please see that change here: 47521514 ) and it gave correct answers for all the three languages on custom-test and I submitted it with G++17 and it got accepted(same as above). The problem I can see is : I was not assigning value as 0 to a key and value to a key 0(unordered map) and was making a while loop with terminating condition being till the time there is value for a key updating key every time to the value and it worked well on my ide(please see the codes above for better reference). Please tell why is this happening or what is the best practice or what to avoid in future.

• +6

 » 17 months ago, # |   +2  for(auto it:plus2) { int it2=it.second; while(plus2[it2]) Here you loop over a hash map while modifying it. If an insertion occurs and results in a rehashing of the container, all iterators are invalidated. Otherwise iterators are not affected. I assume that if you insert zeros in advance, no modification is happening.To avoid this, you could use std::map: No iterators or references are invalidated.
•  » » 17 months ago, # ^ |   0 Thanks :)