In my project I want to insert keys to a map. All new keys should get the value 1.0, but existing keys should be incremented by 1.
Here's the code
vector <string> pairs;
map<string, float> two;
map <string, float>::iterator it;
string a = "a";
string b = "b";
string c = "a";
pairs.push_back(a);
pairs.push_back(b);
pairs.push_back(c);
for(int i=0; i<pairs.size(); i++)
{
it = two.find(string(pairs[i]) );
if(i==0)
{
two[string(pairs[i])]=1.0;
}
else if ( it == two.end() )
{
it->second = it->second + 1.0;
//after this line ^,my compiler stop working
}
else
{
two[string(pairs[i])]=1.0;
}
}
After this, the object should be
a 2
b 1
How can I do so.