list cbegin() and cend() function in C++ STL
- The list::cbegin() is a built-in function in C++ STL which returns a constant random access iterator which points to the beginning of the list. Hence the iterator obtained can be used to iterate container but cannot be used to modify the content of the object to which it is pointing even if the object itself is not constant.
Syntax:
list_name.cbegin()
Parameters: The function does not accept any parameters.
Return value: It returns a constant random access iterator which points to the beginning of the list.
Below programs illustrate the above function:
// C++ program to illustrate the// cbegin() function#include <bits/stdc++.h>usingnamespacestd;intmain(){// declaration of listlist<int> lis = { 5, 6, 7, 8, 9 };// Prints the first elementcout <<"The first element is: "<< *lis.cbegin();// printing list elementscout <<"\nList: ";for(autoit = lis.cbegin(); it != lis.end(); ++it)cout << *it <<" ";return0;}chevron_rightfilter_noneOutput:The first element is: 5 List: 5 6 7 8 9
- The list::cend() is a built-in function in C++ STL which returns a constant random access iterator which points to the end of the list. Hence the iterator obtained can be used to iterate container but cannot be used to modify the content of the object to which it is pointing even if the object itself is not constant.
Syntax:
list_name.cend()
Parameters: The function does not acceps any parameter.
Return value: It returns a constant random access iterator which points to the end of the list.
Below program illustrates the function:
// C++ program to illustrate the// cend() function#include <bits/stdc++.h>usingnamespacestd;intmain(){// declaration of listlist<int> lis = { 10, 20, 30, 40, 50 };// printing list elementscout <<"List: "<< endl;for(autoit = lis.cbegin(); it != lis.cend(); ++it)cout << *it <<" ";return0;}chevron_rightfilter_noneOutput:List: 10 20 30 40 50
Recommended Posts:
- set cbegin() and cend() function in C++ STL
- map cbegin() and cend() function in C++ STL
- multiset cbegin() and cend() function in C++ STL
- match_results cbegin() add cend() in C++ STL
- array::cbegin() and array::cend() in C++ STL
- multimap::cbegin() and multimap::cend() in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- unordered_set cend() function in C++ STL
- unordered_multimap cend() function in C++ STL
- unordered_multiset cend() function in C++ STL
- unordered_multiset cbegin() function in C++ STL
- unordered_multimap cbegin() function in C++ STL
- unordered_set cbegin() function in C++ STL
- forward_list::cend() in C++ STL with Example
- unordered_map cend in C++ STL
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.


