multimap::crbegin() and multimap::crend() in C++ STL

Syntax: 

multimap_name.crbegin()

Parameters: The function does not accept any parameter. 

Return Value: The function returns a constant reverse iterator referring to the last element in the multimap container.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// multimap::crbegin() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    multimap<int, int> mp;
 
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 3, 60 });
    mp.insert({ 4, 20 });
    mp.insert({ 5, 50 });
 
    auto ite = mp.crbegin();
    cout << "The last element is {" << ite->first
    << ", " << ite->second << "}\n";
 
    // prints the elements
    cout << "\nThe multimap in reverse order is: \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) {
        cout << itr->first
             << '\t' << itr->second << '\n';
    }
    return 0;
}
chevron_right

Output: 
The last element is {5, 50}

The multimap in reverse order is: 
KEY    ELEMENT
5    50
4    20
3    60
2    30
1    40




 

Syntax: 



multimap_name.crend()

Parameters: The function does not accept any parameter. 

Return Value: The function returns a constant reverse iterator pointing to the theoretical element before the first element in the multimap.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// multimap::crend() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    multimap<int, int> mp;
 
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 3, 60 });
    mp.insert({ 4, 20 });
    mp.insert({ 5, 50 });
 
    // prints the elements
    cout << "\nThe multimap in reverse order is : \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) {
        cout << itr->first
             << '\t' << itr->second << '\n';
    }
    return 0;
}
chevron_right

Output: 
The multimap in reverse order is : 
KEY    ELEMENT
5    50
4    20
3    60
2    30
1    40




 

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.





The function of education is to teach one to think intensively and to think critically

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.



Improved By : arorakashish0911

Article Tags :
C++
Practice Tags :