STL映射的下标运算符的开销 [英] Overhead of subscript operator for STL maps

查看:69
本文介绍了STL映射的下标运算符的开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含嵌入式char *成员的自定义String类。

复制构造函数,赋值运算符等都正确定义了
。我需要创建一个我的字符串的地图(例如一个名为

MyString的类)和一个整数,即std :: map< MyString,int>。每当我使用subcript []运算符在地图中插入元素时,我就会注意到MyString的复制构造函数被调用更多数字
$ b $比我使用insert()操作的时间b。对于例如对于3

字符串,当我使用下标运算符将它们插入到地图中时,

复制构造函数被调用6次,而在insert()的情况下,

复制构造函数仅被调用3次。有人可以请

解释一下为什么会这样?

I have a custom String class that contains an embedded char* member.
The copy constructor, assignment operator etc. are all correctly
defined. I need to create a map of my string (say a class called
MyString) and an integer i.e. std::map<MyString, int>. Whenever I
insert the elements in the map using the subscript [] operator, I
noticed that the copy constructor for MyString is invoked more number
of times than if I do it using the insert() operation. For e.g. for 3
strings, when I insert them into the map using subscript operator, the
copy constructor is invoked 6 times whereas in the case of insert(),
the copy constructor is only invoked 3 times. Can someone please
explain me the reason why this is so?

推荐答案

10月16日,1:53 * pm ,C ++ Liliput, < aveekmi ... @ gmail.comwrote:
On Oct 16, 1:53*pm, "C++Liliput" <aveekmi...@gmail.comwrote:

我有一个包含嵌入式char *成员的自定义String类。

复制构造函数,赋值运算符等都是正确的

定义的。我需要创建一个我的字符串的地图(例如一个名为

MyString的类)和一个整数,即std :: map< MyString,int>。每当我使用subcript []运算符在地图中插入元素时,我就会注意到MyString的复制构造函数被调用更多数字
$ b $比我使用insert()操作的时间b。对于例如对于3

字符串,当我使用下标运算符将它们插入到地图中时,

复制构造函数被调用6次,而在insert()的情况下,

复制构造函数仅被调用3次。有人可以请

解释一下为什么会这样?
I have a custom String class that contains an embedded char* member.
The copy constructor, assignment operator etc. are all correctly
defined. I need to create a map of my string (say a class called
MyString) and an integer i.e. std::map<MyString, int>. Whenever I
insert the elements in the map using the subscript [] operator, I
noticed that the copy constructor for MyString is invoked more number
of times than if I do it using the insert() operation. For e.g. for 3
strings, when I insert them into the map using subscript operator, the
copy constructor is invoked 6 times whereas in the case of insert(),
the copy constructor is only invoked 3 times. Can someone please
explain me the reason why this is so?



你在哪里打电话给运营商[]?我认为粘贴你的代码可以使问题明确。

Where do you call the operator[]? I think paste your codes could make
the question clear.


C ++ Liliput写道:
C++Liliput wrote:

我有一个包含嵌入式char *成员的自定义String类。

复制构造函数,赋值运算符等都正确定义了
。我需要创建一个我的字符串的地图(例如一个名为

MyString的类)和一个整数,即std :: map< MyString,int>。每当我使用subcript []运算符在地图中插入元素时,我就会注意到MyString的复制构造函数被调用更多数字
$ b $比我使用insert()操作的时间b。对于例如对于3

字符串,当我使用下标运算符将它们插入到地图中时,

复制构造函数被调用6次,而在insert()的情况下,

复制构造函数仅被调用3次。有人可以请

解释一下为什么会这样?
I have a custom String class that contains an embedded char* member.
The copy constructor, assignment operator etc. are all correctly
defined. I need to create a map of my string (say a class called
MyString) and an integer i.e. std::map<MyString, int>. Whenever I
insert the elements in the map using the subscript [] operator, I
noticed that the copy constructor for MyString is invoked more number
of times than if I do it using the insert() operation. For e.g. for 3
strings, when I insert them into the map using subscript operator, the
copy constructor is invoked 6 times whereas in the case of insert(),
the copy constructor is only invoked 3 times. Can someone please
explain me the reason why this is so?



下标运算符创建一个默认的构造对象,然后将传递的对象复制到它上面。因此,您将看到一份操作员电话的副本

和一份副本。


-

Ian Collins

The subscript operator creates a default constructed object then copies
the passed object to it. So you will see a copy for the operator call
and one for the copy in.

--
Ian Collins


10月16日,8:32 * am,Ian Collins< ian-n ... @ hotmail.comwrote:
On Oct 16, 8:32*am, Ian Collins <ian-n...@hotmail.comwrote:

C ++ Liliput写道:
C++Liliput wrote:

我有一个包含嵌入式char *成员的自定义String类。

复制构造函数,赋值运算符等都是正确的

定义的。我需要创建一个我的字符串的地图(例如一个名为

MyString的类)和一个整数,即std :: map< MyString,int>。每当我使用subcript []运算符在地图中插入元素时,我就会注意到MyString的复制构造函数被调用更多数字
$ b $比我使用insert()操作的时间b。对于例如对于3

字符串,当我使用下标运算符将它们插入到地图中时,

复制构造函数被调用6次,而在insert()的情况下,

复制构造函数仅被调用3次。有人可以请

解释一下为什么会这样?
I have a custom String class that contains an embedded char* member.
The copy constructor, assignment operator etc. are all correctly
defined. I need to create a map of my string (say a class called
MyString) and an integer i.e. std::map<MyString, int>. Whenever I
insert the elements in the map using the subscript [] operator, I
noticed that the copy constructor for MyString is invoked more number
of times than if I do it using the insert() operation. For e.g. for 3
strings, when I insert them into the map using subscript operator, the
copy constructor is invoked 6 times whereas in the case of insert(),
the copy constructor is only invoked 3 times. Can someone please
explain me the reason why this is so?



下标运算符创建一个默认的构造对象,然后将传递的对象复制到它上面。 *因此,您将看到操作员调用的副本

和副本中的一个。


The subscript operator creates a default constructed object then copies
the passed object to it. *So you will see a copy for the operator call
and one for the copy in.



下标运算符创建默认构造对象仅适用于

*值*,但不适用于std :: map<>的键。


实际上,std :: map< Key ,Valueinternally存储std :: pair< Key,Value>。

当调用std :: map<> :: operator []并且没有这样的键时,

它创建一个临时的std :: pair< Key,Value(其中Value是默认值

初始化)然后复制临时std :: pair< Key,Valueinto

新创建的(树)节点,因此2调用

Key的复制构造函数。另一方面,std :: map :: insert()接受std :: pair< Key,

Value>,用于初始化节点的副本,因此1复制

构造函数调用。


-

Max

The subscript operator creates a default constructed object only for
the *value*, but not for the key of std::map<>.

In fact, std::map<Key, Valueinternally stores std::pair<Key, Value>.
When std::map<>::operator[] is called and there has been no such key,
it creates a temporary std::pair<Key, Value(where Value is default
initialised) and then copies that temporary std::pair<Key, Valueinto
the newly created (tree) node, thus 2 calls to the copy constructor of
Key. std::map::insert(), on the other hand, accepts std::pair<Key,
Value>, which is used to initialise the node''s copy of it, thus 1 copy
constructor call.

--
Max


这篇关于STL映射的下标运算符的开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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