前段时间,我和同事讨论了如何在STL映射中插入值。I prefer map[key] = value;因为它让人感觉自然,读起来清晰,而他更喜欢地图。插入(std:: make_pair(关键字,值))。
我只是问了他,我们都不记得为什么插入更好,但我相信这不仅仅是一个风格偏好,而是有一个技术原因,如效率。SGI STL引用简单地说:“严格地说,这个成员函数是不必要的:它只是为了方便而存在。”
有人能告诉我原因吗,还是我只是在做梦?
前段时间,我和同事讨论了如何在STL映射中插入值。I prefer map[key] = value;因为它让人感觉自然,读起来清晰,而他更喜欢地图。插入(std:: make_pair(关键字,值))。
我只是问了他,我们都不记得为什么插入更好,但我相信这不仅仅是一个风格偏好,而是有一个技术原因,如效率。SGI STL引用简单地说:“严格地说,这个成员函数是不必要的:它只是为了方便而存在。”
有人能告诉我原因吗,还是我只是在做梦?
当前回答
如果默认构造函数的性能影响不是问题,看在上帝的份上,请使用可读性更好的版本。
:)
其他回答
下面是另一个例子,如果键值存在,操作符[]会覆盖键值,但是.insert不会覆盖键值。
void mapTest()
{
map<int,float> m;
for( int i = 0 ; i <= 2 ; i++ )
{
pair<map<int,float>::iterator,bool> result = m.insert( make_pair( 5, (float)i ) ) ;
if( result.second )
printf( "%d=>value %f successfully inserted as brand new value\n", result.first->first, result.first->second ) ;
else
printf( "! The map already contained %d=>value %f, nothing changed\n", result.first->first, result.first->second ) ;
}
puts( "All map values:" ) ;
for( map<int,float>::iterator iter = m.begin() ; iter !=m.end() ; ++iter )
printf( "%d=>%f\n", iter->first, iter->second ) ;
/// now watch this..
m[5]=900.f ; //using operator[] OVERWRITES map values
puts( "All map values:" ) ;
for( map<int,float>::iterator iter = m.begin() ; iter !=m.end() ; ++iter )
printf( "%d=>%f\n", iter->first, iter->second ) ;
}
需要注意的是,您也可以使用Boost。分配:
using namespace std;
using namespace boost::assign; // bring 'map_list_of()' into scope
void something()
{
map<int,int> my_map = map_list_of(1,2)(2,3)(3,4)(4,5)(5,6);
}
map::insert()的一个问题是,如果键已经存在于映射中,它不会替换某个值。我见过Java程序员编写的c++代码,他们希望insert()的行为与Java中的Map.put()相同,其中值被替换。
insert()和operator[]之间的区别已经在其他答案中很好地解释了。然而,c++ 11和c++ 17分别引入了新的std::map插入方法:
c++ 11提供了emplace(),在einpoklum的评论和GutiMac的回答中也提到过。 c++ 17提供了insert_or_assign()和try_emplace()。
让我简单总结一下“新”插入方法:
emplace(): When used correctly, this method can avoid unnecessary copy or move operations by constructing the element to be inserted in place. Similar to insert(), an element is only inserted if there is no element with the same key in the container. insert_or_assign(): This method is an "improved" version of operator[]. Unlike operator[], insert_or_assign() doesn't require the map's value type to be default constructible. This overcomes the disadvantage mentioned e.g. in Greg Rogers' answer. try_emplace(): This method is an "improved" version of emplace(). Unlike emplace(), try_emplace() doesn't modify its arguments (due to move operations) if insertion fails due to a key already existing in the map.
有关insert_or_assign()和try_emplace()的更多详细信息,请在这里看到我的回答。
Coliru上的简单示例代码
关于std::map还有一点需要注意:
关联(nonExistingKey);将在map中创建一个新条目,键指向初始化为默认值的nonExistingKey。
当我第一次看到它时(当我的头撞到一个讨厌的遗留bug时),这把我吓坏了。没想到会这样。对我来说,这看起来像一个get操作,我没有预料到“副作用”。当从你的地图获取时,更倾向于map.find()。