C++ STL 映射 - 插入或更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2786649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:40:10  来源:igfitidea点击:

STL map - insert or update

c++stl

提问by CodeJunkie

I have a map of objects and I want to update the object mapped to a key, or create a new object and insert into the map. The update is done by a different function that takes a pointer to the object (void update(MyClass *obj))

我有一个对象映射,我想更新映射到一个键的对象,或者创建一个新对象并插入到映射中。更新由一个不同的函数完成,该函数采用指向对象的指针 (void update(MyClass *obj))

What is the best way to "insert or update" an element in a map?

在地图中“插入或更新”元素的最佳方法是什么?

回答by Terry Mahaffey

The operator[]   

operator[]   

回答by CB Bailey

With something like the following snippet:

使用类似于以下代码段的内容:

std::map<Key, Value>::iterator i = amap.find(key);

if (i == amap.end())
    amap.insert(std::make_pair(key, CreateFunction()));
else
    UpdateFunction(&(i->second));

If you want to measure something that might improve performance you might want to use .lower_bound()to find where an entry and use that as a hint to insert in the case where you need to insert a new object.

如果您想衡量一些可能会提高性能的东西,您可能希望使用它.lower_bound()来查找条目的位置,并将其用作在需要插入新对象的情况下插入的提示。

std::map<Key, Value>::iterator i = amap.lower_bound(key);

if (i == amap.end() || i->first != key)
    amap.insert(i, std::make_pair(key, CreateFunction()));
                                       // Might need to check and decrement i.
                                       // Only guaranteed to be amortized constant
                                       // time if insertion is immediately after
                                       // the hint position.
else
    UpdateFunction(&(i->second));

回答by ablaeul

The operator[]already does, what you want. See the referencefor details.

operator[]已经这样做,你想要什么。有关详细信息,请参阅参考资料

回答by RvdK

something like:

就像是:

map<int,MyClass*> mymap;
map<int,MyClass*>::iterator it;

MyClass* dummy = new MyClass();
mymap.insert(pair<int,MyClass*>(2,dummy));

it = mymap.find(2);
update(it.second);

here a nice reference link

这里有一个不错的参考链接

回答by InsanityPants

The return value of insert is "a pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool denoting whether the insertion took place."

insert 的返回值是“一对由插入元素(或阻止插入的元素)的迭代器和指示是否发生插入的布尔值组成。”

Therefore you can simply do

因此你可以简单地做

auto result = values.insert({ key, CreateFunction()});
if (!result.second)
    UpdateFunction(&(result.first->second));

NOTE: Since your question involved raw pointers, and you said you wanted your Update function to take a pointer, I have made that assumption in my snippet. Assume that CreateFunction() returns a pointer and UpdateFunction() expects a pointer.

注意:由于您的问题涉及原始指针,并且您说您希望 Update 函数采用指针,因此我在我的代码片段中做出了这个假设。假设 CreateFunction() 返回一个指针,而 UpdateFunction() 需要一个指针。

I'd strongly advise against using raw pointers though.

不过,我强烈建议不要使用原始指针。