STL 映射 - 插入或更新

发布于 2024-08-31 20:00:14 字数 130 浏览 3 评论 0原文

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

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

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))

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

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

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

发布评论

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

评论(6

救赎№ 2024-09-07 20:00:14

operator[]     

The operator[]    

农村范ル 2024-09-07 20:00:14

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

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

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

如果您想测量可能提高性能的内容,您可能需要使用 .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));

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.

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));
拥醉 2024-09-07 20:00:14

类似于:

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);

这里有一个很好的参考链接

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

2024-09-07 20:00:14

operator[] 已经完成了您想要的操作。有关详细信息,请参阅参考

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

烂人 2024-09-07 20:00:14

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

因此你可以简单地做

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

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

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

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."

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.

I'd strongly advise against using raw pointers though.

初熏 2024-09-07 20:00:14

在 C++17 中,函数insert_or_assign 如果不存在则插入,如果存在则更新。

In C++17, function insert_or_assign insert if not existing and update if there.

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