Skip to content
Related Articles

Related Articles

multiset crbegin() and crend() function in C++ STL

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 24 Jun, 2022

The multiset::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the last element in the container. The iterator cannot be used to modify the elements in the multiset container. The iterators can be increased or decreased to traverse the set accordingly. 

Syntax: 

constant_reverse_iterator multiset_name.crbegin()

Parameters: The function does not take any parameter.

Return value: The function returns a constant iterator pointing to the last element in the container. 

Below programs illustrate the multiset::crbegin() method.

C++




// C++ program to demonstrate the
// multiset::crbegin() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
 
    int arr[] = { 14, 10, 15, 11, 10 };
 
    // initializes the set from an array
    multiset<int> s(arr, arr + 5);
 
    cout << "The last element: " << *(s.crbegin()) << endl;
    // prints all elements in set
    for (auto it = s.crbegin(); it != s.crend(); it++)
        cout << *it << " ";
 
    return 0;
}

Output: 

The last element: 15
15 14 11 10 10

 

The multiset::crend() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the position just before the first element in the container. The iterator cannot be used to modify the elements in the multiset container. The iterators can be increased or decreased to traverse the set accordingly. 

Syntax: 

constant_reverse_iterator multiset_name.crend()

Parameters: The function does not take any parameter.

Return value: The function returns a constant iterator pointing to the position just before the first element in the multiset container. 

Below programs illustrate the multiset::crend() method. 

C++




// C++ program to demonstrate the
// multiset::crend() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
 
    int arr[] = { 14, 12, 15, 11, 10, 10, 16, 16 };
 
    // initializes the set from an array
    multiset<int> s(arr, arr + 8);
 
    // prints all elements in set
    for (auto it = s.crbegin(); it != s.crend(); it++)
        cout << *it << " ";
 
    return 0;
}

Output: 

16 16 15 14 12 11 10 10

 

Time Complexity: O(1)

Auxiliary Space: O(1)
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!