如何将键和值插入到 cpp 中另一个地图内的地图?

How to insert key and value to map that is inside another map in cpp?

我有这张地图:

std::map<std::string, std::map<std::string, std::string> > objects;

据我所知,当我写 objects[key] 时,它会替换已经存在的键的值,但在这里我希望它在不存在的情况下添加到第二个映射中。

Example:

    Current MAP :("key1",map(<"key_a","value_a">,<"key_c","value_d">))

input:
 "key1", "key_e","value_f" 
relsult:
("key1",map(<"key_a","value_a">,<"key_c","value_d">,<"key_e","value_f">))

input :
"key1", "key_e","value_g" 
(replace the value from "value_f" to "value_g")
result:
("key1",map(<"key_a","value_a">,<"key_c","value_d">,<"key_e","value_g">))

and if I get "key2" -> it will insert new key with empty map

我可以这样做吗:

objects[key].insert(std::make_pair(key2,value2))

由于某种原因它没有工作

能请教一下吗?

也许你的意思是这样的? http://www.cplusplus.com/reference/map/map/operator%5B%5D/ http://www.cplusplus.com/reference/map/map/count/

std::map<std::string, std::map<std::string, std::string> > objects;
if (objects.count("your_key") > 0)
{   // insert a key ("another_key") into map
    objects["your_key"]["another_key"] = "some value";
} else
{   // create the new map for the key
    objects["your_key"] = std::map<std::string, std::string>();
}

您可以使用方法insert。这是一个演示程序

#include <iostream>
#include <string>
#include <map>

int main() 
{
    std::map<std::string, std::map<std::string, std::string>> objects;

    objects["key1"] = { { "key_a", "value_a" }, { "key_c", "value_d" } };

    objects["key1"].insert( { "key_e", "value_f" } ); 

    for ( const auto &p1 : objects )
    {
        std::cout << "\"" << p1.first << "\": ";
        for ( const auto &p2 : p1.second )
        {
            std::cout << "{ \"" << p2.first 
                      << "\", \"" << p2.second
                      << "\" }" << ' ';
        }
        std::cout << std::endl;
    }

    objects["key1"]["key_e"] = "value_g"; 

    for ( const auto &p1 : objects )
    {
        std::cout << "\"" << p1.first << "\": ";
        for ( const auto &p2 : p1.second )
        {
            std::cout << "{ \"" << p2.first 
                      << "\", \"" << p2.second
                      << "\" }" << ' ';
        }
        std::cout << std::endl;
    }

    return 0;
}

程序输出为

"key1": { "key_a", "value_a" } { "key_c", "value_d" } { "key_e", "value_f" } 
"key1": { "key_a", "value_a" } { "key_c", "value_d" } { "key_e", "value_g" } 

As far as I know, when I write objects[key] it will replace the value of the key already exists, but here I want it to add to the second map if the pair doesn't exist.

错了:如果你写 objects[key] 是永远不会替换任何东西;相反:

  • 如果 objects 尚未包含 key,它会使用该键在映射中插入一个新对,并且对于它使用默认构造的值(在此案例为空)std::map<std::string, std::string>。然后它 returns 对值的引用。

  • 如果 objects 确实包含 key,它 returns 对现有值("inner" 映射)的引用而不更改或替换任何内容.

can I do this: objects[key].insert(std::make_pair(key2,value2))

你可以,但如果 [key][key2] 已经存在,则不会将值更新为 value2,我认为这就是你想要的以及你为什么说 "it didn't work".

您所要做的就是:

objects[key][key2] = value2;

因为 objects[key] 永远不会替换现有的 map,但如果 key 缺失则默认构造一个,然后 key2 要么在 "inner" 地图需要更新 或创建它 ,这一切都挂在一起(如果我正确理解您的要求)。