C++ List::end() Function



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

The end() function cannot be used to modify the element or the list container. In C++, iterators are used to point at the memory addresses of the STL(standard template library) containers.

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

Syntax

Following is the syntax of the std::list::end() function −

iterator end();

Parameters

  • It does not accept any parameter.

Return Value

This function returns an iterator to the element following the last element of the list.

Example 1

In the following program, we are using the C++ std::list::end() function to retrieve an iterator to the element following the last element of the current list {1, 2, 3, 4, 5}.

#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<<" ";
   }
   auto it = num_list.end();
   cout<<"\nThe iterator of the last element: ";
   cout<<*it;
}

Output

Following is the output of the above program −

List elements are: 1 2 3 4 5 
The 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::end() function. Here, we are creating a list(type char) named vowels with the values {'a', 'e', 'i', 'o', 'u'}. Then using the end() function, we are trying to retrieve an 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.end();
   cout<<"\nThe iterator of the last element: ";
   cout<<*it;
}

Output

This will generate the following output −

List elements are: a e i o u 
The 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 end() function, we are trying to retrieve an 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.end();
   cout<<"\nThe iterator of the last element: ";
   cout<<*it;
}

Output

The above program produces the following output −

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

Example 4

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

In this example, we use the end() function along with the for loop to loop through the current container {10, 20, 30, ,40 ,50} and retrieve an iterator of all elements in the container elements.

#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<<" ";
   }
   cout<<"\nThe iterator of an elements are : ";
   for(auto i = num_list.begin(); i!=num_list.end(); i++) {
      cout<<*i<<" ";
   }
}

Output

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

List elements are: 10 20 30 40 50 
The iterator of an elements are : 10 20 30 40 50 
Advertisements