
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
set crbegin and crend Function in C++ STL
In this article we are going to discuss the set::crbegin() and set::crend() functions in C++ STL, their syntax, working and their return values.
What is Set in C++ STL?
Sets in C++ STL are the containers which must have unique elements in a general order. Sets must have unique elements because the value of the element identifies the element. Once added a value in set container later can’t be modified, although we can still remove or add the values to the set. Sets are used as binary search trees.
What is set::crbegin()?
crbegin() function is an inbuilt function in C++ STL, which is defined in <set> header file. crbegin() implies constant reverse begin iterator, means the reverse of the cbegin which was constant begin iterator, in other words the function crbegin() will return the iterator which is pointing to the last element of the set container associated with the function. Like other iterators this also can be used to modify the set. This can be just used to traverse the set container.
Syntax
constant_iterator name_of_set.crbegin();
Parameter
This function does’nt accept any parameter.
Return value
This function returns the iterator which is pointing to the last element of the set container.
Example
Input: set<int> myset = {1, 2, 3, 4, 5}; myset.crbegin(); Output: 5
Example
#include <bits/stdc++.h> using namespace std; int main(){ int arr[] = {1, 2, 3, 4, 5}; set<int> ch(arr, arr + 5); for (auto i = ch.crbegin(); i!= ch.crend(); i++) cout << *i << " "; return 0; }
Output
If we run the above code it will generate the following output
5 4 3 2 1
What is set::crend()
crend() function is an inbuilt function in C++ STL, which is defined in <set> header file. crend() implies constant reverse end iterator, means the reverse of the cend which was constant end iterator, in other words the function crend() will return the iterator which is pointing to the position just before the first position of the set container associated with the function. Like other iterators this also can be used to modify the set. This can be just used to traverse the set container.
Syntax
constant_iterator name_of_set.crend();
Parameter
This function does not accept any parameter.
Return value
This function returns the iterator which is pointing to the position just before the first position of the set container which is associated with the function.
Example
Input: set<int> myset = {1, 2, 3, 4, 5}; myset.crend(); Output: 9 //random number before the first element in the set container.
Example
#include <bits/stdc++.h> using namespace std; int main(){ int arr[] = {3, 5, 8, 1, 9}; set<int> ch(arr, arr + 5); for(auto i = ch.crbegin(); i!= ch.crend(); i++) cout << *i<< " "; return 0; }
Output
If we run the above code it will generate the following output
9 8 5 3 1