In this article we will be discussing the working, syntax and examples of multiset::crbegin() and multiset::crend() function 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.
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.
ms_name.crbegin();
The function accepts no parameter.
This function returns a constant iterator which is pointing to the last element of the container.
std::multiset<int> mymultiset = {1, 2, 2, 3, 4}; mymultiset.crbegin();
4
#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; }
First element fetched using crbegin() function: 60 60 50 40 30 20 10
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.
ms_name.crend();
The function accepts no parameter.
This function returns a constant iterator which is pointing to the element previous than the first of the container.
std::multiset<int> mymultiset = {1, 2, 2, 3, 4}; mymultiset.crend();
error
#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; }
Elements in the list are: 60 50 40 30 20 10