我正在解决一个 DSA 问题,并在 C++ 标准模板库中观察到一个稍微奇怪的行为:
vector<int> v = {1, 7, 8, 3, 12};
unordered_map<int, unordered_set<int>> ump;
for (int i=0; i<v.size(); i++) {
unordered_set<int> us;
us.insert(56);
ump.insert(mp(v[i], us));
}
for (int i=0; i<v.size(); i++) {
unordered_set<int> us = ump.find(v[i])->second;
us.insert(67);
}
for (auto it = ump.begin(); it != ump.end(); it++) {
cout << it->first << ": ";
for (auto ait = it->second.begin(); ait!=it->second.end(); ait++) {
cout << *ait << ' ';
}
cout << '\n';
}
Run Code Online (Sandbox Code Playgroud)
这里的输出是:
vector<int> v = {1, …Run Code Online (Sandbox Code Playgroud)