C++ List::cend() Function



The C++ std::list::cend() function is used to retrieve a constant iterator to the element following the last element of the list.

The iterator obtained by this member function can be used to iterate the container but cannot be used to modify the content of the container to which it is pointing even if the object itself is not constant.

The cend() function is similar to the end() function, where the end() function returns an iterator to the element following the last element of the list, whereas the cend() function returns a constant iterator following to the last element of the list.

Syntax

Following is the syntax of the C++ std::list::cend() function −

const_iterator cend() const;

Parameters

  • It does not accept any parameter.

Return Value

This function returns a constant random access iterator following to the last element of the list.

Example 1

In the following program, we are using the C++ std::list::cend() function to retrieve a constant iterator to the element following the last element of the current list {10, 20, 30, 40, 50}.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<int> num_list = {10, 20, 30, 40, 50};
   cout<<"List elements are: ";
   for(int n : num_list) {
      cout<<n<<" ";
   }
   auto it = num_list.cend();
   cout<<"\nA constant iterator of the last element: ";
   cout<<*it;
}

Output

Following is the output of the above program −

List elements are: 10 20 30 40 50 
A constant iterator of the last element: 5

Example 2

Apart from the int-type list element, you can also retrieve an iterator of any other type list element like char and string list content.

Following is another example of the C++ std::list::cend() function. Here, we are creating a list(type char) named vowels with the values {'a', 'e', 'i', 'o', 'u'}. Then using the cend() function, we are trying to retrieve a constant iterator following to the last element in the current list.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<char> vowels = {'a', 'e', 'i', 'o', 'u'};
   cout<<"List elements are: ";
   for(char v : vowels) {
      cout<<v<<" ";
   }
   auto it = vowels.cend();
   cout<<"\nA constant iterator of the last element: ";
   cout<<*it;
}

Output

This will generate the following output −

List elements are: a e i o u 
A constant iterator of the last element: 

Example 3

If the list type is a string.

In this example, we are creating a list(type string) named fruits with the values {"Orange", "Grapes", "Banana", "Apple"}. Then, using the cend() function, we are trying to retrieve a constant iterator following to the last element in the current list.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<string> fruits  =  {"Orange", "Grapes", "Banana", "Apple"};
   cout<<"List elements are: ";
   for(string s : fruits ) {
      cout<<s<<" ";
   }
   auto it = fruits.cend();
   cout<<"\nThe constant iterator of the last element: ";
   cout<<*it;
}

Output

The above program produces the following output −

List elements are: Orange Grapes Banana Apple 
The constant iterator of the last element: 
Segmentation fault

Example 4

Using for loop along with the cend() function to retrieve the iterator of the container element.

In this example, we use the cend() function along with the for loop to loop through the current container {1, 2, 3, ,4 ,5} and retrieve a constant iterator of all elements in the container elements.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<int> num_list = {1, 2, 3, 4 ,5};
   cout<<"List elements are: ";
   for(int n : num_list) {
      cout<<n<<" ";
   }
   cout<<"\nThe constant iterator of an elements are : ";
   for(auto i = num_list.begin(); i!=num_list.cend(); i++) {
      cout<<*i<<" ";
   }
}

Output

On executing the above program, it will produce the following output −

List elements are: 1 2 3 4 5 
The constant iterator of an elements are : 1 2 3 4 5
Advertisements