@Moschops: Calm down, he's trying to understand a difficult concept.
@adam2016: If you have a vector of ints, the vector iterator will iterate over ints. Thus, dereferencing a vector iterator will give you the int value. Now a map is made of
pairs. In other words, every element of a map is a pair<T1, T2> type. In your case, the map is made up of pair<string, int> elements. Map iterators will iterator over pairs, and thus when you dereference the map iterator, you get pair<string, int> element.
Every pair has a first and second member. In order to access the int part, you need to first dereference the iterator to get the pair, then access the second member of the pair.
1 2 3 4
|
it // iterator 'pointing' to a pair<string, int> element
*it // gives you pair<string, int>
(*it).second // gives you the int value
it->second // fancy way of writing the previous line.
|
This is why the first program works (successfully accessing the pair, then getting the int value in that pair), while the second one fails (as its trying to cout a pair<string, int> which does not make sense).
As for the third program, maps are not random access containers like vectors, thus they do no support operator+ or operator-. You can, however, use the increment or decrement operators.