This is a simple script I was working on, but I failed to understand why it was behaving unexpectedly.
Basically, I had an array of integers with duplicates, and I wanted to store the number of times an element occurred in the array along with the element's value in an unordered_map,
Then, for each entry in the map {k, v}, I needed to determine if k + 1 existed in the array, and if so, do something with it. Below you can see the code.
vector<int> A = {1, 1, 3, 2, 5, 3};
for (int i = 0; i < A.size(); ++i) m[A[i]]++;
int ans = 0;
for (const auto& e: m) {
if (m[e.first + 1] > 0) ans = max(ans, e.second + m[e.first + 1]);
}
Everything seemed to work. However, when k + 1 did not exist in the unordered_map, the loop would just terminate, and I do not understand why.
According to c++ documentation, the operator [] inserts a new element if it does not exist . But that does not tell me anything about the loop just not working.
I suspect it has something to do with the fact that I am modifying the unordered_map inside the loop. If this is the case, could you guys elaborate more on that?
I really appreciate your help.