Supposed I have a graph defined in such way,
unordered_map<int, unordered_set<int>> graph = {
{ 0, { 1, 2, 3 } },
{ 1, { 3, 4, 5 } },
{ 2, { 6, 5, 4 } }
};
And I want to empty the hash set of each entry in graph. I do two options here,
A. for(auto v = graph.begin(); v != graph.end(); ++v) v->second.clear();
B. for(auto v : graph) v.second.clear();
I see A works but B does not. I do not quite understand. My theory is, the way B is doing, v is a copy of the actual element. So it cannot clear the actual hash set.
Need help. Thanks!