C++ 根据键字符串使用迭代器更改嵌套映射的int值

C++ 根据键字符串使用迭代器更改嵌套映射的int值,c++,dictionary,nested,key,increment,C++,Dictionary,Nested,Key,Increment,我现在有下面的地图 typedef map<int, int> inner; map<string, inner> example; example["hello"][1] = 100; example["hi"][2] = 200; //need to increment 2 to 3, and increase by 200 example["bye"][3] = 300; example["ace"][4] = 400; typedef映射内部; 地图示例;

我现在有下面的地图

typedef map<int, int> inner;
map<string, inner> example;

example["hello"][1] = 100; 
example["hi"][2] = 200; //need to increment 2 to 3, and increase by 200
example["bye"][3] = 300;
example["ace"][4] = 400; 
typedef映射内部;
地图示例;
示例[“你好”][1]=100;
示例[“hi”][2]=200//需要增加2到3,增加200
示例[“再见”][3]=300;
示例[“ace”][4]=400;
我想做的是修改内部映射的值,我目前正在尝试以下操作

int size; //second int in inner map
int count; //first int in inner map

for (map<string, inner>::iterator it = example.begin(); it != example.end(); it++)
    {
        cout << it->first << " " ;
        for (inner::iterator it2 = it->second.begin(); it2 != it->second.end(); it2++)
        { 
            if (it->first.compare("hi")) //if at key "hi" 
            { 
                count = it2->first + 1;//increment by one
                size = it2->second + 200;//add 200
                example.erase("hi"); //remove this map entry
                example["hi"][count] = size; //re-add the map entry with updated values
            }

        }
int大小//内部映射中的第二个int
整数计数//内部映射中的第一个int
对于(map::iterator it=example.begin();it!=example.end();it++)
{
cout first second.begin();it2!=it->second.end();it2++)
{ 
if(it->first.compare(“hi”)//if在键“hi”处
{ 
count=it2->first+1;//递增1
size=it2->second+200;//添加200
example.erase(“hi”);//删除此映射项
示例[“hi”][count]=size;//使用更新的值重新添加映射项
}
}

我尝试了几种不同的方法,但我确实觉得我不了解指针是如何工作的。我的输出显示计数值是2,大小是300(在“hello”键处修改的值)

除了注释中给出的信息外:

如果将大小和计数映射到字符串,请将大小和计数命名为有意义的值,并使其成为结构:

struct Stats {
    int size;
    int count;
};

std::map<std::string, Stats> example;

example["hello"] = {1, 100};
example["hi"] = {2, 200};
如果要打印所有元素,请执行以下操作:

for (auto& i : example) {
    std::cout << i.first << " = {"
              << i.second.size << ", "
              << i.second.count << "}\n";
}
for(自动&i:示例){

std::难道你不能修改密钥,你可能想要
std::map
我该如何向它添加值?我正在尝试:example.insert(“hello”,make_pair(1100));它不起作用
example.insert({“hello”,“11000});
example[“hello”]={1100}
太好了!我还有一个问题,然后它就顺利了。我注意到成对的循环不使用迭代器,所以我该如何更改我的嵌套for循环?谢谢各位!你们不需要嵌套的循环。现在只有一个容器。只要去掉嵌套的循环。
it2->first
变成
it->second.first
,依此类推。it->second.size+=200只是将大小增加到202,而不是增加到400。我不明白为什么会这样。@ChandlerPavia,因为大小从2开始计算,从200开始计算……来吧,伙计。
for (auto& i : example) {
    std::cout << i.first << " = {"
              << i.second.size << ", "
              << i.second.count << "}\n";
}