I have a function in which it checks for the characters and number of times it is repeated in a string.
It is stored as (for example)string is "hello" [h]=>1 [e]=>1 [l]=>2 [o]=1
Whenever a letter occurs more than once I need to update it.
I tried using
it->second = it->second+1;
But it doesn't works How can I do that?
Full code is
int fn(string a) {
map<char,int> mymap;
for(int i=0;i<a.size();i++)
{
std::map<char, int>::iterator it = mymap.find(i);
if(it!=mymap.end())
{
//say i need to update occurrence from 1 to 2 or 2 to 3...
it->second = it->second+1;//(how can i do that)
}
else
mymap.insert(pair<char,int>(a[i],1));
}
std::map<char,int>::iterator i;
for(i=mymap.begin();i!=mymap.end();i++)
{
cout<<i->first<<i->second;
}
}
Aucun commentaire:
Enregistrer un commentaire