I hate to add to the myriad of questions about undefined behavior on stack overflow, but this problem has mystified me.
When using a range-based for loop on an unordered_map that is created inline, I get an unexpected result. When using the same loop on an unordered_map that was first assigned to a variable, I get the expected result.
I would expect both loops to print 1, but that is not what I observe.
Any help understanding what is happening would be appreciated. Thanks!
I'm running on Debian 10 with g++ 8.3.0
#include <algorithm>
#include <unordered_map>
#include <iostream>
#include <vector>
int main() {
for (const int i : std::unordered_map<int, std::vector<int>> 0}.at(0)) {
std::cout << i << std::endl; //prints 0
}
std::unordered_map<int, std::vector<int>> map {
{0, std::vector<int> {1}}
};
for (const int i : map.at(0)) {
std::cout << i << std::endl; //prints 1
}
}
Aucun commentaire:
Enregistrer un commentaire