
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set cbegin and cend Function in C++ STL
In this article we are going to discuss the set::cend() and set::cbegin() functions in C++ STL, their syntax, working and their return values.
What is Set in C++ STL?
Sets in C++ STL are the containers which must have unique elements in a general order. Sets must have unique elements because the value of the element identifies the element. Once added a value in set container later can’t be modified, although we can still remove or add the values to the set. Sets are used as binary search trees.
What is set::cbegin():
cbegin() function is an inbuilt function in C++ STL, which is defined in header file. This function returns the constant iterator which is pointing to the first element in the set container. Since all the iterators in the set container are the constant iterators, they can’t be used to modify the content all we can use them for traverse among the elements of set container by increasing or decreasing the iterator.
Syntax
constant_iterator name_of_set.cbegin();
Parameter
This function does not accept any parameter.
Return value
This function returns the constant_iterator, which is past the end of the sequence.
Example
Input: set<int> set_a = {18, 34, 12, 10, 44}; set_a.cbegin(); Output: Beginning element in the set container: 10
Example
#include <iostream> #include <set> using namespace std; int main (){ set<int> set_a = {18, 34, 12, 10, 44}; cout << "Beginning element in the set container: "; cout<< *(set_a.cbegin()); return 0; }
Output
If we run the above code then it will generate the following output −
Beginning element in the set container: 10
Example
#include <iostream> #include <set> using namespace std; int main (){ set<int> set_a = {18, 34, 12, 10, 44}; cout << "set_a contains:"; for (auto it=set_a.cbegin(); it != set_a.cend(); ++it) cout << ' ' << *it; cout << '\n'; return 0; }
Output
If we run the above code then it will generate the following output −
set_a contains: 10 12 18 34 44
What is set::cend()
cend() function is an inbuilt function in C++ STL, which is defined in header file. This function returns the constant iterator of the element which is past the last element in the set container. Since all the iterators in the set container are the constant iterators, they can’t be used to modify the content all we can use them is for traverse among the elements of set container by increasing or decreasing the iterator.
Syntax
constant_iterator name_of_set.cend();
Parameter
This function doesn’t accept any parameter.
Return value
This function returns the constant_iterator, which is past the end of the sequence.
Example
Input: set<int> set_a = {18, 34, 12, 10, 44}; set_a.end(); Output: Past to end element: 5
set::cend() is used with cbegin() or begin() to iterate through the whole set, because it points to the element past to the last element in the container.
Example
#include <iostream> #include <set> using namespace std; int main (){ set<int> set_a = {18, 34, 11, 10, 44}; cout << "Past to end element: "; cout<< *(set_a.cend()); return 0; }
Output
If we run the above code then it will generate the following output −
Past to end element: 5 We will get a random value
Example
#include <iostream> #include <set> using namespace std; int main (){ set<int> set_a = {18, 34, 12, 10, 44}; cout << " set_a contains:"; for (auto it= set_a.cbegin(); it != set_a.cend(); ++it) cout << ' ' << *it; cout << '\n'; return 0; }
Output
If we run the above code then it will generate the following output −
set_a contains: 10 12 18 34 44