jeudi 3 décembre 2015

Can I insert an element to a given iterator for a map?

I have a std::map. I find a lower bound of a key and make sure the key is not used in the map. And can I insert a element to a given iterator for a map? If so how?

map<int, double> m;
int key = 1;
auto itr = m.lower_bound(key);
if (itr == m.end() || itr->first != key)
    m.insert(itr, make_pair(key, 3.14)); // how is the performance? Any better way?

Aucun commentaire:

Enregistrer un commentaire