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

标签 c++ boost join dictionary std

我有一个 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/

相关文章:

c++ - Lapackpp 与 Boost BLAS

sql - 我应该使用 SQL JOIN 还是 IN 子句?

oracle - 在哪里做连接以展平表..? Hive或Oracle

c++ - 在 MySQL/Connector C++ 中使用 SQL 连接

c++ - GDB分步调试两个线程

c++ - 从没有二维数组的矩阵文件中读取坐标?

c++ - 作为好友的成员函数 : Is the book Lippman 5th wrong?

C++ matlab引擎c++回调函数

c++ - 在 C++ Builder 中编译 Boost 库时的警告

c++ - 使用 Boost property_tree 读取 JSON