The rend() function is an inbuilt function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first key-value pair in the map(which is considered its reverse end).
Syntax:
map_name.rend()
Parameters:The function does not take any parameter.
Return Value: The function returns a reverse iterator pointing to the theoretical element right before the first element in the map.
Note: Reverse iterators iterate backwards i.e when they are increased they move towards the beginning of the container.
The following programs illustrates the function.
Program 1:
// C++ program to illustrate map::rend() function #include <iostream> #include <map> using namespace std; int main() { map<char, int> mymap; // Insert pairs in the multimap mymap.insert(make_pair('a', 1)); mymap.insert(make_pair('b', 3)); mymap.insert(make_pair('c', 5)); // Show content for (auto it = mymap.rbegin(); it != mymap.rend(); it++) { cout << it->first << " = " << it->second << endl; } return 0; } |
c = 5 b = 3 a = 1
Program 2:
// C++ program to illustrate map::rend() function #include <iostream> #include <map> using namespace std; int main() { map<char, int> mymap; // Insert pairs in the multimap mymap.insert(make_pair('a', 1)); mymap.insert(make_pair('b', 3)); mymap.insert(make_pair('c', 5)); // Get the iterator pointing to // the preceding position of // 1st element of the map auto it = mymap.rend(); // Get the iterator pointing to // the 1st element of the multimap it--; cout << it->first << " = " << it->second; return 0; } |
a = 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- deque rend() function in C++ STL
- list rbegin() and rend() function in C++ STL
- multiset rbegin() and rend() function in C++ STL
- vector rbegin() and rend() function in C++ STL
- multimap rend in C++ STL
- set::rbegin() and set::rend() in C++ STL
- array::rbegin() and array::rend() in C++ STL
- Function Overloading vs Function Overriding in C++
- How to call function within function in C or C++
- Difference between Virtual function and Pure virtual function in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- div() function in C++
- log() function in C++
- exp() function C++
- fma() function in C++
- map key_comp() function in C++ STL
- wctob() function in C++
- wcscoll() function in C++
- isinf() function in C++
- Modulus function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

