I have the following program:
1. Container A contains pairs of numbers
2. Container M contains key->value pairs
3. Iterating over A, i check if values in its second column match keys in M.
4. If so, that value in A gets replaced by the value in M.
Code:
std::vector<std::pair<int, int>> A;
std::unordered_map<int, int> M;
size_t solve() // solve is method, has access to A & M
{
auto m = M.cend();
size_t changes = 0;
for(auto& a : A)
{
m = M.find(a.second);
if(m != M.cend()) // match
{
a.second = m->second;
++changes;
}
}
return changes;
}
Now my problem is as follows: Each time I run the program, the changes value returned by solve is identical on the first call (good), but different on the following ones (WAT!), such that:
Code:
int main()
{
for(int i = 0; i < N; ++i)
cout << "#" << i << ": " << solve() << endl;
}
./program:
#1: 25,000,008
#2: 12,000 (finds less, expected)
#3: 5,000 (^...)
./program:
#1: 25,000,008 (same, good)
#2: 7,000 (what the hell!)
#3: 3,400 (...)
./program:
#1: 25,000,008 (same, good)
#2: 63,000 (what in the world!?)
#3: 9,300 (...)
The input data is always the same, and no part of the program intentionally contains random behavior. Any idea what could cause this? Am I mis-using iterators?