C++ set rbegin() Function
Example
Get the last element of a set:
set<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
auto it = cars.rbegin();
cout << *it;
Definition and Usage
The rbegin() function returns a reverse iterator to the last element of a set. A reverse iterator is an iterator (a pointer-like object that points to an element) that moves backwards: ++ moves it to the previous element.
The elements in a set are sorted, so the last element is the largest value. For strings, this is the last one in alphabetical order.
The rbegin function is defined in the <set> header file.
Tip: Use rbegin() together with rend() to loop through a set in reverse order.
Syntax
set.rbegin()
Parameter Values
None.
Technical Details
| Returns: | A reverse iterator pointing to the last element of the set. |
|---|
Related Pages
Read more about sets in our Sets Tutorial.