Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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




 


 



Last Updated : 20 Nov, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads