c ++ 11 operator []是否等效于地图插入? [英] Is c++11 operator[] equivalent to emplace on map insertion?

查看:76
本文介绍了c ++ 11 operator []是否等效于地图插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于C ++ 11,以下各项之间仍然存在性能差异吗?

For C++11, is there still a performance difference between the following?

(以 std :: map< Foo,std :: vector< Bar>> 为例)

map[key] = myVector and map.emplace(key, myVector)

我没有弄清楚的部分是operator []的确切内部。到目前为止,我的理解是(当键不存在时):

The part I'm not figuring out is the exact internal of operator[]. My understanding so far has been (when key doesn't exist):


  1. 创建一个新键,并将关联的空默认矢量放置在内部地图

  2. 返回关联的空向量的引用

  3. 将myVector分配给该引用?

第3点是我无法理解的部分,首先如何为引用分配新值?

The point 3 is the part I couldn't understand, how can you assign a new value to a reference in the first place?

尽管我无法对第3点进行排序,但我认为某种程度上仅需要复制/移动。假设C ++ 11足够聪明,知道它将进行一次移动操作,那么整个 []赋值是否已经比insert()便宜?它几乎等同于emplace()吗? ----默认构造并移动内容,而不是直接构造具有内容的矢量?

Though I cannot sort through point 3 I think somehow there's just a copy/move required. Assuming C++11 will be smart enough to know it's gonna be a move operation, is this whole "[]" assignment then already cheaper than insert()? Is it almost equivalent to emplace()? ---- default construction and move content over, versus construct vector with content directly in place?

推荐答案

有一个很多两者之间的差异。

There are a lot of differences between the two.

如果您使用 operator [] ,则 map 默认构造该值。 operator [] 的返回值将是此默认构造的对象,然后将使用 operator = 为其分配值。

If you use operator[], then the map will default construct the value. The return value from operator[] will be this default constructed object, which will then use operator= to assign to it.

如果使用 emplace 地图直接使用您提供的参数构造值。

If you use emplace, the map will directly construct the value with the parameters you provide.

因此, operator [] 方法将始终使用两阶段施工。如果默认构造函数的速度很慢,或者复制/移动的构造速度比复制/移动的分配速度快,则可能会出现问题。

So the operator[] method will always use two-stage construction. If the default constructor is slow, or if copy/move construction is faster than copy/move assignment, then it could be problematic.

但是,如果提供的键已经存在,则emplace 不会替换该值。而 operator [] 后跟 operator = 将始终替换该值,无论是否存在该值。

However, emplace will not replace the value if the provided key already exists. Whereas operator[] followed by operator= will always replace the value, whether there was one there or not.

也有其他差异。如果复制/移动抛出, emplace 保证地图 不会被更改。相比之下, operator [] 将始终插入默认的构造元素。因此,如果以后的复制/移动分配失败,则 map 已被更改。该键将以默认构造的 value_type 存在。

There are other differences too. If copying/moving throws, emplace guarantees that the map will not be changed. By contrast, operator[] will always insert a default constructed element. So if the later copy/move assignment fails, then the map has already been changed. That key will exist with a default constructed value_type.

确实,性能并不是您应该考虑的第一件事关于何时决定使用哪个。您需要首先关注它是否具有所需的行为。

Really, performance is not the first thing you should be thinking about when deciding which one to use. You need to focus first on whether it has the desired behavior.

C ++ 17将提供 insert_or_assign ,效果为 map [] = v ; ,但除​​了 insert / emplace 的安全性。

C++17 will provide insert_or_assign, which has the effect of map[] = v;, but with the exception safety of insert/emplace.


首先如何为引用分配新值?

how can you assign a new value to a reference in the first place?

从根本上说,与分配给任何 const 参考:

It's fundamentally no different from assigning to any non-const reference:

int i = 5;
int &j = i;
j = 30;
i == 30; //This is true.

这篇关于c ++ 11 operator []是否等效于地图插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆