How does "*"
affect the iterator returned?
The dereference (*
) means that the value stored in last
is not the iterator, but a copy of what's stored at that position in the map, which incidentally is the last node of the map. It works much in the same way as dereferencing a pointer, you get the value pointed by it, not the pointer itself.
Does the value_comp compares *i++
and the last value or iterator?
Yes it does. It compares the two keys, the one "pointed by" i
and the one stored in last
. The method will return true as long as the first argument key is at a lower position on the map than the key of the second argument.
In your example, when the keys match the cycle ends.
As per the description of the method:
It returns a comparison object that can be used to compare two elements to get whether the key of the first one goes before the second.
The comparison object returned is an object of the member type map::value_compare
, which is a nested class that uses the internal comparison object to generate the appropriate comparison functional class.
Side note:
It may be clear that *i++
also returns the content of i
, and the iterator i
is being incremented at each iteration, but this expression can cause confusion, you could use brackets to avoid it, like so *(i++)
.