The easy way is obviosuly
std::map<int,std::unique_ptr<something>> mymap;
auto f = mymap.find(5);
std::unique_ptr<something> myptr;
if (f == mymap.end())
mymap.insert({5, std::move(myptr)});
However, this doesn't look too efficient, as I have to find the key in the map twice. One to check if the key doesn't exist, and the insert function will also do the same.
If I simply use mymap.insert({5, std::move(myptr)});
then my unique ptr (myptr) is gone if pair.second
returns false (key already exists).
EDIT:
Apparently the answer is on C++17, with try_emplace
, and it's already available in the compiler I'm using (vs2015) and since I'm working on a personal project, I can afford to use it.
If you are not going to store nullptr
s in your map then you can do it like this:
auto& r = mymap[5];
if ( r == nullptr )
r = std::move(myptr);
The standard trick is to search for the insertion point:
auto f = mymap.lower_bound(5);
if ((f == mymap.end()) || mymap.key_comp()(5, f->first)) {
mymap.insert(f, {5, std::move(myptr)}); // or mymap.emplace_hint(f, 5, std::move(myptr))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With