when I have a std::map, is there an elegant way to at the same time:
- insert / edit an element given its key
- get an iterator to the inserted element
The best way I found that prevent doing 2 look-up in the map is:
std::map<int, int> myMap;
//do some stuff with the map
std::map<int,int>::iterator it = myMap.insert(std::pair<int, int>(0,0)).first;
it->second = 0; //necessary because insert does not overwrite the values
Is it possible to do both of those things in a single statement / line?
thanks
Alas, the STL functions and containers don’t always do what you’d expect. Here are two generic versions, the first more like your code above: