- 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:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
list< int > lis = { 5, 6, 7, 8, 9 };
cout << "The first element is: " << *lis.cbegin();
cout << "\nList: " ;
for ( auto it = lis.cbegin(); it != lis.end(); ++it)
cout << *it << " " ;
return 0;
}
|
Output:
The first element is: 5
List: 5 6 7 8 9
Time Complexity: O(1)
Auxiliary Space: O(1)
- 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 accepts any parameter.
Return value: It returns a constant random access iterator which points to the end of the list.
Below program illustrates the function:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
list< int > lis = { 10, 20, 30, 40, 50 };
cout << "List: " << endl;
for ( auto it = lis.cbegin(); it != lis.cend(); ++it)
cout << *it << " " ;
return 0;
}
|
Output:
List:
10 20 30 40 50
Time Complexity: O(1)
Auxiliary Space: O(1)
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 :
13 Jun, 2022
Like Article
Save Article