地图STL C++的迭代器它不起作用?

use*_*502 -4 c++ stl map

我有奇怪的问题.我声明了map :: iterator的迭代器,但它不起作用.哪里有问题?

 string name "John";
    int count = 200;
    map<string,int> store;
    map<string,int>::iterator it;

it = store.find( name );

            if ( it != store.end() )
            {
                it->second += count;
            } else
            {
                store.insert( make_pair (name, count) );
            }
Run Code Online (Sandbox Code Playgroud)

jua*_*nza 5

我不确定你的意思是"不起作用",但你可以大大简化你的代码:

store[name] += count;
Run Code Online (Sandbox Code Playgroud)

说明:store[name]如果没有一个条目,将添加一个条目,并使用值构造映射值.在这种情况下,由于您的映射类型是int,它将被初始化为零.