Reputation: 179
I have an implementation of a map where the ID is being stored as value and marks as key. This enables me to take advantage of the auto sorting in maps and lets me identify the ID of the element with highest marks.
for(map<int, int>::iterator i = marks.begin(); i != marks.end(); ++i)
cout << i->first << "\t" << i->second << endl;
cout << marks.rbegin()->second << endl;
cout << marks.end()->second << endl;
produces this output:
312 3
420 4
512 2
752 1
1
420
The input sequence was the increasing order of values. Why does end()
not display "1" but instead displays the key of the last pair inputted? What's the difference between rbegin()
and end()
?
Upvotes: 6
Views: 3375
Reputation: 117971
rbegin
is actually the last element of your container.
end
is one past the end of the container.
So marks.end()->second
is undefined behavior.
Upvotes: 17