Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 30 Sep, 2022
Improve Article
Save Article

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

constant_iterator set_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. 
Program to demonstrate the set::crbegin() method:
 

CPP




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

 

Output: 

15 14 12 11 10

 

 

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

constant_iterator set_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 set container. 
Program to demonstrate the set::crend() method:
 

CPP




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

 

Output: 

15 14 12 11 10

 

Let us see the differences in a tabular form -:

 set crbegin() set crend() 
1.It is used to return a const_reverse_iterator pointing to the last element in the containerIt is used to return a const_reverse_iterator pointing to the element that would theoretically precede 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 changes.Its iterator validity does not changes.

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!