Difference between rbegin and final function in the standard library

I have a map implementation where the identifier is stored as a value and marked as a key. This allows me to use automatic sorting on maps and allows me to identify the identifier of the item with the 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 conclusion:

 312 3 420 4 512 2 752 1 1 420 

The input sequence was an increase in the order of values. Why doesn't end() display "1", but instead display the key of the last pair entered? What is the difference between rbegin() and end() ?

+5
source share
1 answer

rbegin is actually the last element of your container. end is located behind the end of the container.

So marks.end()->second is undefined behavior.

+12
source

Source: https://habr.com/ru/post/1263980/


All Articles