Open In App

unordered_set cend() function in C++ STL

Last Updated : 05 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The unordered_set::cend() method is a built-in function in C++ STL which is used to return a const_iterator pointing to past-the-end element in the unordered_set container or in one of it's bucket. This function does not directly point to any element in the container. It is only used to denote the end of a container or the open end of a range as in [cbegin, cend). 

Note: A const_iterator can only be used to access elements, it cannot modify the elements present in the container. 

Syntax:

unordered_set_name.cend(n);

Parameter: This function accepts a single parameter n. This is an optional parameter and specifies the bucket number. If this parameter is not passed then the cend() method will return a const_iterator pointing to the position just after the last element of the container and if this parameter is passed then the cend() method will return a const_iterator pointing to the position just after the last element in a specific bucket in the unordered_set container. 

Return Value: This function returns a const_iterator pointing to the position just after the last element in the container or a specified bucket in the container. Below programs illustrate the unordered_set::cend() function: 

Program 1

CPP
// C++ program to illustrate the 
// unordered_set::cend() function 

#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

    unordered_set<int> sampleSet; 

    // Inserting elements in the std 
    sampleSet.insert(5); 
    sampleSet.insert(10); 
    sampleSet.insert(15); 
    sampleSet.insert(20); 
    sampleSet.insert(25); 

    // Here, the cend() method is used to 
    // iterate in the range of elements 
    // present in the unordered_set container 
    cout << "Elements present in sampleSet are: \n"; 
    for (auto itr = sampleSet.cbegin(); itr != sampleSet.cend(); 
        itr++) { 
        cout << *itr << endl; 
    } 

    return 0; 
} 
Output:
Elements present in sampleSet are: 
25
5
10
15
20

Program 2

CPP
// C++ program to illustrate the 
// unordered_set::cend() function 

#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

    unordered_set<string> sampleSet; 

    // Inserting elements 
    sampleSet.insert("Welcome"); 
    sampleSet.insert("To"); 
    sampleSet.insert("GeeksforGeeks"); 
    sampleSet.insert("Computer Science Portal"); 
    sampleSet.insert("For Geeks"); 

    // Here, the cend() method is used to 
    // iterate in the range of elements 
    // present in the unordered_set container 
    cout << "Elements present in sampleSet are: \n"; 
    for (auto itr = sampleSet.cbegin(); itr != sampleSet.cend(); 
        itr++) { 
        cout << *itr << endl; 
    } 

    return 0; 
} 
Output:
Elements present in sampleSet are: 
Welcome
To
GeeksforGeeks
For Geeks
Computer Science Portal

Time complexity: O(1)


Similar Reads