samedi 23 décembre 2017

Figure out whether an item has been added or not when using emplace_hint

Since emplace_hint method of set / map returns only an iterator figuring out whether item has been actually added is not as convenient as when using emplace or insert. Right now I just get container size before / after and compare them:

::std::map<int, int> items{};
const auto initial_items_count{items.size()};
const auto it_item{items.emplace_hint(items.begin(), 0, 5)};
if(items.size() != initial_items_count)
{
    // perform additional actions...
}

It can be wrapped up into a helper returning pair, but maybe there is an existing simpler approach?

Aucun commentaire:

Enregistrer un commentaire