Open In App

list back() function in C++ STL

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.

Syntax

list_name.back();

Parameters

Return Value

Exception

Example

The below program illustrates the list::back() function.






// CPP program to illustrate the
// list::back() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Initialization of list
    list<int> demo_list;
 
    // Adding elements to the list
    demo_list.push_back(10);
    demo_list.push_back(20);
    demo_list.push_back(30);
 
    // prints the last element of demo_list
    cout << demo_list.back();
 
    return 0;
}

Output
30

Time Complexity: O(1)
Auxiliary Space: O(1)



Article Tags :
C++