std::map 索引和插入调用之间的差异

发布于 2024-08-08 12:42:20 字数 197 浏览 2 评论 0 原文

std::map 的索引重载运算符和 insert 方法调用有什么区别?

即:

some_map["x"] = 500;

vs.

some_map.insert(pair<std::string, int>("x", 500));

What is the difference between the index overloaded operator and the insert method call for std::map?

ie:

some_map["x"] = 500;

vs.

some_map.insert(pair<std::string, int>("x", 500));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抠脚大汉 2024-08-15 12:42:20

我相信 insert() 不会覆盖现有值,并且可以通过测试返回的迭代器/对值中的 bool 值来检查操作的结果。

对下标运算符 [] 的赋值只会覆盖那里的任何内容(如果插入一个条目 。

如果您不期望这种行为并且不适应它,则插入和 [] 运算符中的任何一个都可能会导致问题

例如 with insert:

std::map< int, std::string* > intMap;
std::string* s1 = new std::string;
std::string* s2 = new std::string;
intMap.insert( std::make_pair( 100, s1 ) ); // inserted
intMap.insert( std::make_pair( 100, s2 ) ); // fails, s2 not in map, could leak if not tidied up

和 with [] 运算符:

std::map< int, std::string* > intMap;
std::string* s1 = new std::string;
std::string* s2 = new std::string;
intMap[ 100 ] = s1; // inserted
intMap[ 100 ] = s2; // inserted, s1 now dropped from map, could leak if not tidied up

我认为这些是正确的,但还没有编译它们,所以可能有语法错误

I believe insert() will not overwrite an existing value, and the result of the operation can be checked by testing the bool value in the iterator/pair value returned

The assignment to the subscript operator [] just overwrites whatever's there (inserting an entry if there isn't one there already)

Either of the insert and [] operators can cause issues if you're not expecting that behaviour and don't accommodate for it.

Eg with insert:

std::map< int, std::string* > intMap;
std::string* s1 = new std::string;
std::string* s2 = new std::string;
intMap.insert( std::make_pair( 100, s1 ) ); // inserted
intMap.insert( std::make_pair( 100, s2 ) ); // fails, s2 not in map, could leak if not tidied up

and with [] operator:

std::map< int, std::string* > intMap;
std::string* s1 = new std::string;
std::string* s2 = new std::string;
intMap[ 100 ] = s1; // inserted
intMap[ 100 ] = s2; // inserted, s1 now dropped from map, could leak if not tidied up

I think those are correct, but haven't compiled them, so may have syntax errors

随波逐流 2024-08-15 12:42:20

对于map,前一个(operator[])表达式将始终用新提供的值替换键值对的值部分。如果新的键值对尚不存在,则将插入该对。

相反,如果映射中尚不存在包含所提供的键部分的键值对,则 insert 只会插入新的键值对。

For a map, the former (operator[]) expression will always replace the value part of the key-value pair with the new supplied value. A new key-value pair will be inserted if one doesn't already exist.

In contrast, insert will only insert a new key-value pair if a key-value pair with the supplied key part does not already exist in the map.

夜无邪 2024-08-15 12:42:20

除了 map::operator[] 将替换现有值之外,operator[] map::将创建并添加到地图< /strong> 在替换发生之前要替换的默认现有值(map::operator[]() 调用必须返回对某些内容的引用)。对于创建成本昂贵的项目,这可能是一个性能问题。

请参阅 map::operator[] 和 map::insert 之间仔细选择”。 stackoverflow.com/amzn/click/com/0321334876" rel="noreferrer">Scott Meyers 的有效 STL

In addition to the fact that map::operator[] will replace an existing value is that operator[] map::will create and add to the map a default existing value to replace before the replacement occurs (the map::operator[]() call has to return a reference to something). For items that are expensive to create this could be a performance issue.

See "Item 24: Choose carefully between map::operator[] and map::insert when efficiency is important" in Scott Meyers' Effective STL.

缪败 2024-08-15 12:42:20

insert方法插入到map中,而重载索引运算符如果在map中,则返回键为key_value的元素,如果不在map中,则将其插入。

The insert method inserts into the map, while the overloaded index operator will return the element with the key key_value if it is in the map, if it is not already in the map then it will insert it.

沧桑㈠ 2024-08-15 12:42:20

只是添加到 Michael Burr 的答案,这本书您应该寻找的是 Scott Meyer 的 <Effective STL>不是 ,因为 Michael 错误地链接了它。

表达式

m[k] = v;

 检查键 k 是否已经在映射中。如果不是的话,那就是
添加,连同 v 作为其对应值。如果 k 已经在
map,其关联值更新为v。

(添加新kv时)

我们首先默认构造一个,然后立即赋值
它是一个新的值。如果构建一个效率明显更高
使用我们想要的值而不是默认构造
<值对象>然后做作业,我们最好更好替换我们的
使用运算符[](包括其伴随的构造加上
赋值)通过直接调用插入

更新现有的 k 的 v)

我们现在明白,当执行“添加”时,插入是更多
比运算符[]高效。 当我们这样做时,情况就相反了
更新
,即当等效密钥已存在于
地图。

Just to add to Michael Burr's answer, the book you should be looking for is Scott Meyer's <Effective STL> not the <Effective C++>, as Michael has linked it wrongly.

the expression

m[k] = v;

  checks to see if the key k is already in the map. If not, it’s
added, along with v as its corresponding value. If k is already in the
map, its associated value is updated to v.

(when adding new k-v)

We first default-construct a <value obj>, then we immediately assign
it a new value. If it’s measurably more efficient to construct a
<value obj>
with the value we want instead of default-constructing the
<value obj> and then doing the assignment, we’d be better off replacing our
use of operator[] (including its attendant construction plus
assignment) with a straightforward call to insert:

(updating existing k's v)

we now understand that when an “add” is performed, insert is more
efficient than operator[]. The situation is reversed when we do an
update
, i.e., when an equivalent key is already in the
map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文