Skip to content
Related Articles

Related Articles

Improve Article

list back() function in C++ STL

  • Last Updated : 18 Jun, 2018

The list::back() function in C++ STL returns a direct reference to the last element in the list container. This function is different from the list::end() function as the end() function returns only the iterator to the last element.



Parameters: This function does not accepts any parameter.

Return Value: This function returns a direct reference to the last element in the list container demo_list.

Exception: There is as no such exception in this function but calling this function on an empty list container creates an undefined behavior in C++.

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

// CPP program to illustrate the
// list::assign() function
#include <bits/stdc++.h>
using namespace std;
int main()
    // Initialization of list
    list<int> demo_list;
    // Adding elements to the list
    // prints the last element of demo_list
    cout << demo_list.back();
    return 0;
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :