简体   繁体   English

使用复制昂贵的类作为std :: map中的映射值是否有效?

[英]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. 对于矢量,我将选择boost :: shared_ptr。 But for std::map, I am not sure whether it is worthwhile. 但是对于std :: map,我不确定是否值得。

Documentation for std::map::insert() says http://en.cppreference.com/w/cpp/container/map/insert : std::map::insert()文档std::map::insert() 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 对于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. 但是在这种情况下,我将使用std::map::emplace()方法和/或移动语义,而不是使用共享指针来使代码复杂化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM