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

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of multiset::crbegin() and multiset::crend() function in C++ STL.

What is a multiset in C++ STL?

Multisets are the containers similar to the set container, meaning they store the values in the form of keys same like a set, in a specific order.

In multiset the values are identified as keys as same as sets. The main difference between multiset and set is that the set has distinct keys, meaning no two keys are the same, in multiset there can be the same keys value.

Multiset keys are used to implement binary search trees.

What is multiset::crbegin()?

multiset::crbegin() function is an inbuilt function in C++ STL, which is defined in header file. crbegin() means constant reverse begin function, means this function returns the constant iterator pointing to the last element of the multiset container. This function is the reverse version of multiset::cbegin()

The constant iterator can be just used for iterating through the multiset container, this can’t make changes in the multiset container.

Syntax

ms_name.crbegin();

Parameters

The function accepts no parameter.

Return value

This function returns a constant iterator which is pointing to the last element of the container.

Example

Input

std::multiset<int> mymultiset = {1, 2, 2, 3, 4};
mymultiset.crbegin();

Output

4

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   int arr[] = {10, 20, 30, 40, 50, 60};
   multiset<int> check(arr, arr + 6);
   cout<<"First element fetched using crbegin() function: "<<*(check.crbegin()) << endl;
   for(auto i = check.crbegin(); i!= check.crend(); i++)
      cout << *i << " ";
   return 0;
}

Output

First element fetched using crbegin() function: 60
60 50 40 30 20 10

What is multiset::crend()?

multiset::crend() function is an inbuilt function in C++ STL, which is defined in <set> header file. crend() means constant end function, means this function returns the constant iterator pointing to element preceding the first element of the multiset container. This is reverse version of cend()

The constant iterator can be just used for iterating through the multiset container, this can’t make changes in the multiset container.

Syntax

ms_name.crend();

Parameters

The function accepts no parameter.

Return value

This function returns a constant iterator which is pointing to the element previous than the first of the container.

Example

Input

std::multiset<int> mymultiset = {1, 2, 2, 3, 4};
mymultiset.crend();

Output

error

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   int arr[] = {10, 20, 30, 40, 50, 60};
   multiset<int> check(arr, arr + 6);
   cout<<"Elements in the list are: ";
   for(auto i = check.crbegin(); i!= check.crend(); i++)
      cout << *i << " ";
   return 0;
}

Output

Elements in the list are: 60 50 40 30 20 10
raja
Published on 17-Apr-2020 08:56:49
Advertisements