How to update the value of a key in std::map
after using the find
method?
I have a map and iterator declaration like this:
map <char, int> m1;
map <char, int>::iterator m1_it;
typedef pair <char, int> count_pair;
I'm using the map to store the number of occurrences of a character.
I'm using Visual C++ 2010.
std::map::find
returns an iterator to the found element (or to theend()
if the element was not found). So long as themap
is not const, you can modify the element pointed to by the iterator:You can also do like this-
I would use the operator[].
So using this technique it becomes really easy to read all the character from a stream and count them:
You can use
std::map::at
member function, it returns a reference to the mapped value of the element identified with key k.