Open In App
Related Articles

set cbegin() and cend() function in C++ STL

Improve Article
Improve
Save Article
Save
Like Article
Like

The set::cbegin() is a built-in function in C++ STL which returns a constant iterator pointing to 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.cbegin()

Parameters: The function does not accept any parameters.

Return value: The function returns a constant iterator pointing to the first element in the container. 

Program to demonstrate the set::cbegin() method.

C++




// C++ program to demonstrate the
// set::cbegin() 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.cbegin(); it != s.cend(); it++)
        cout << *it << " ";
 
    return 0;
}


Output: 

10 11 12 14 15



 

set::cend() is a built-in function in C++ STL which returns a constant iterator pointing to the position past 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 in the set accordingly. 

Syntax: 

constant_iterator set_name.cend()

Parameters: The function does not accept any parameters.

Return value: The function returns a constant iterator pointing to the position past the last element in the container in the container. 

Program to demonstrate the set::cend() method.

C++




// C++ program to demonstrate the
// set::cend() 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.cbegin(); it != s.cend(); it++)
        cout << *it << " ";
 
    return 0;
}


Output: 

10 11 12 14 15



 

 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 20 Nov, 2020
Like Article
Save Article
Previous
Next
Similar Reads