unordered_set cend() function in C++ STL

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

Elements present in sampleSet are: 
25
5
10
15
20

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

Elements present in sampleSet are: 
Welcome
To
GeeksforGeeks
For Geeks
Computer Science Portal

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.