set::rbegin() and set::rend() in C++ STL
- set::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container.
Syntax:
reverse_iterator set_name.rbegin()
Parameters: The function does not take any parameter.
Return value: The function returns a reverse iterator pointing to the last element in the container.
Program to demonstrate the set::rbegin() method:
Program 1:
// CPP program to demonstrate the// set::rbegin() function#include <bits/stdc++.h>usingnamespacestd;intmain(){intarr[] = { 14, 12, 15, 11, 10 };// initializes the set from an arrayset<int> s(arr, arr + 5);set<int>::reverse_iterator rit;// prints all elements in reverse orderfor(rit = s.rbegin(); rit != s.rend(); rit++)cout << *rit <<" ";cout <<"\nThe last element in set is "<< *(s.rbegin());return0;}Output:15 14 12 11 10 The last element in set is 15
- set::rend() in an inbuilt function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first element in the set container.
Syntax:
reverse_iterator set_name.rend()
Parameter: The function does not accepts any parameter.
Return value: The function returns a reverse iterator pointing to the theoretical element right before the first element in the set container.
Below programs illustrate the function:
Program 1:
// CPP program to demonstrate the// set::rend() function#include <bits/stdc++.h>usingnamespacestd;intmain(){intarr[] = { 4, 3, 5, 1, 2 };// initializes the set from an arrayset<int> s(arr, arr + 5);set<int>::reverse_iterator rit;// prints all elements in reverse orderfor(rit = s.rbegin(); rit != s.rend(); rit++)cout << *rit <<" ";return0;}Output:5 4 3 2 1
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.



