我有一个 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_new
至 class_map
如果没有 class_description
有了这样的name
在 class_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/