my question is that I have made multimap.Here is the partial code.
if(binary_search(final.begin() , final.end() , answer ) )
{
final[answer] =
}
else
{
final.insert(pair<string,int>(answer , 1 ) );
}
Here answer is a string and final is a multimap of <string,int>
. Now what I want is that if the string answer is present then increment(modify) the value of int. How can I do this?
I want to increment the value of int by one if the string element is already present?
Regards.
If you want keys to be unique then use map
. Then you can simply do final[answer]++
. Note that map::operator[]
will insert the key into the map if it doesn't exists already. The value will be default constructed during this insertion and the reference to this default constructed value is returned. If the key exists already then it returns the reference to the value element.