Iterator of map STL C++ it doens't work? -
i have strange problem. declareded iterator of map::iterator doesn't work. problem?
string name "john"; int count = 200; map<string,int> store; map<string,int>::iterator it; = store.find( name ); if ( != store.end() ) { it->second += count; } else { store.insert( make_pair (name, count) ); }
the code wrote should work on updated c++ compiler.
i think forget include <map>
code. see live code:
#include <iostream> #include <string> #include <map> using namespace std; int main() { string name = "john"; int count = 200; map<string, int> store; map<string, int>::iterator it; = store.find(name); if (it != store.end()) { it->second += count; } else { store.insert(make_pair(name, count)); cout << "adding new value" << endl; } }
output
adding new value
the method std::map::find
returns iterator , code caught fine , use it.
Comments
Post a Comment