I just spent ~2 on trying to figure out a bug introduced to my code due to the usage of auto iteration for containers. I started using it couple of days ago, without doing much background check, just because I found it easier to write.
I have the following map: std::map<int, VectorList>, where VectorList is just a typedef std::vector<double> VectorList.
I wanted to perform .clear() operation on the std::vector<double> of the VectorList.
I tried the following:
std::map<int, VectorList> map;
for(auto elem : map)
{
elem.second.clear();
}
and it did not work. The clear operation was not being performed on the VectorList. However when I was performing .empty() check on it, it would return True.
Then I went back to this approach:
for(std::map<int, VectorList>::iterator elem = map.begin(); elem != map.end(); ++elem)
{
elem->second.clear();
}
And everything worked as expected.
Question:
Why auto iteration does not perform the .clear() operation as expected? Can this be achieved with auto iteration at all?
Aucun commentaire:
Enregistrer un commentaire