user869210
user869210

Reputation: 231

Is it efficient to use copy-expensive class as the mapped value in std::map?

In a nutshell, is it efficient to use

std::map<int, std::set<int> > instead of
std::map<int, boost::shared_ptr<std::set<int>>?

For vector, i will choose boost::shared_ptr. But for std::map, I am not sure whether it is worthwhile.

Upvotes: 0

Views: 98

Answers (1)

Slava
Slava

Reputation: 44238

Documentation for std::map::insert() says http://en.cppreference.com/w/cpp/container/map/insert:

No iterators or references are invalidated.

For std::map::erase() http://en.cppreference.com/w/cpp/container/map/erase

References and iterators to the erased elements are invalidated. Other references and iterators are not affected.

which means that either insert nor erase does not move objects around, so you should not worry about expense of copy, except the case that you need to insert already created big object into a map. But in this case I would use std::map::emplace() method and/or move semantics rather than complicating code with shared pointer.

Upvotes: 3

Related Questions