Skip to content
Related Articles
Open in App
Not now

Related Articles

list end() function in C++ STL

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 20 Jun, 2018
Improve Article
Save Article

The list::end() is a built-in function in C++ STL which is used to get an iterator to past the last element. By past the last element it is meant that the iterator returned by the end() function return an iterator to an element which follows the last element in the list container. It can not be used to modify the element or the list container.

This function is basically used to set a range along with the list::begin() function.



Parameters: This function does not accept any parameter, it simply returns an iterator to past the last element.

Return Value: This function returns an iterator to the element past the last element of the list.

Below program illustrates the list::end() function.

// CPP program to illustrate the
// list::end() function
#include <bits/stdc++.h>
using namespace std;
int main()
    // Creating a list
    list<int> demoList;
    // Add elements to the List
    // using end() to get iterator 
    // to past the last element
    list<int>::iterator it = demoList.end();
    // This will not print the last element
    cout << "Returned iterator points to : " << *it << endl;
    // Using end() with begin() as a range to
    // print all of the list elements
    for (auto itr = demoList.begin();
         itr != demoList.end(); itr++) {
        cout << *itr << " ";
    return 0;


Returned iterator points to : 4
10 20 30 40

Note: This function works in constant time complexity.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!