Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Difficulty Level : Basic
  • Last Updated : 29 Nov, 2020

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:
 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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

 

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :