gpt4 book ai didi

c++ - 如何更新这样的 map 结构?

转载 作者:行者123 更新时间:2023-11-28 03:48:16 29 4
gpt4 key购买 nike

我有一个 std::map<boost::shared_ptr<some_class>, class_description> class_map;其中 class_description是:

//Each service provides us with rules
struct class_description
{
//A service must have
std::string name;
// lots of other stuff...
};

我还有一个 std::map<boost::shared_ptr<some_class>, class_description> class_map_new;

我需要插入对 <boost::shared_ptr<some_class>, class_description>来自 class_map_newclass_map如果没有 class_description有了这样的nameclass_map前。怎么办?

最佳答案

std::map::insert 不允许重复,因此您可以简单地尝试插入新值:

//Each service provides us with rules
struct class_description
{
//A service must have
std::string name;
// lots of other stuff...
};

std::map<boost::shared_ptr<some_class>, class_description> class_map;
std::map<boost::shared_ptr<some_class>, class_description> class_map_new;

// insert the new values into the class_map
// using C++0x for simplicity...
for(auto new_obj = class_map_new.cbegin(), end = class_map_new.cend();
new_obj != end; ++new_obj)
{
auto ins_result = class_map.insert(*new_obj);

if(false == ins_result.second)
{
// object was already present,
// ins_result.first holds the iterator
// to the current object
}
else
{
// object was successfully inserted
}
}

关于c++ - 如何更新这样的 map 结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676669/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com