set::rbegin() and set::rend() in C++ STL Last Updated : 19 Jun, 2018 Comments Improve Suggest changes 11 Likes Like Report 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 // CPP program to demonstrate the // set::rbegin() function #include <bits/stdc++.h> using namespace std; int main() { int arr[] = { 14, 12, 15, 11, 10 }; // initializes the set from an array set<int> s(arr, arr + 5); set<int>::reverse_iterator rit; // prints all elements in reverse order for (rit = s.rbegin(); rit != s.rend(); rit++) cout << *rit << " "; cout << "\nThe last element in set is " << *(s.rbegin()); return 0; } 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 // CPP program to demonstrate the // set::rend() function #include <bits/stdc++.h> using namespace std; int main() { int arr[] = { 4, 3, 5, 1, 2 }; // initializes the set from an array set<int> s(arr, arr + 5); set<int>::reverse_iterator rit; // prints all elements in reverse order for (rit = s.rbegin(); rit != s.rend(); rit++) cout << *rit << " "; return 0; } Output: 5 4 3 2 1 Create Quiz Comment T Twinkl Bajaj Follow 11 Improve T Twinkl Bajaj Follow 11 Improve Article Tags : C++ STL CPP-Functions cpp-set Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like