Open In App

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

Last Updated : 29 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report
  • multimap::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator referring to the last element in the multimap container. Since multimap container contains the element in an ordered way, crbegin() will point to that element that will come last according to the container’s sorting criterion.

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.

Simple Example :

C++




#include <vector>
#include <iostream>
 
int main() {
  std::vector<int> vec {1, 2, 3, 4, 5};
 
  // Iterate through the elements of vec in reverse order
  for (auto it = vec.crbegin(); it != vec.crend(); ++it) {
    std::cout << *it << ' ';
  }
  std::cout << std::endl;
 
  return 0;
}


Output

5 4 3 2 1 

C++




// 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;
}


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
  • multimap::crend() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the theoretical element before the first element in the multimap. Since multimap container contains the element in an ordered way, crend() will point to the element theoretically before the first element according to the container’s sorting criterion.

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.

C++




// 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;
}


Output

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

Let us see the differences in a tabular form -:

  multimap::crbegin()  multimap::crend() 
1. It is used to return a const_reverse_iterator pointing to the last element in the container It is used to return a const_reverse_iterator pointing to the theoretical element preceding the first element in the container 
2.

Its syntax is -:

const_reverse_iterator crbegin();

Its syntax is -:

const_reverse_iterator crend();

3. It does not take any parameters. It does not take any parameters.
4. Its complexity is constant. Its complexity is constant.
5. Its iterator validity does not change. Its iterator validity does not change.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads