List Back Function

C++ List back

C++ List back function returns the last element of the list. It provides the direct reference to the element.

Difference between back and end function

The end function returns an iterator to the element while back function returns a direct reference to the same element.

Syntax

Example

reference back();

Parameter

It does not contain any parameter.

Return value

It returns the direct reference of the last element.

Example

Let's see a simple example

Example

#include <iostream>
#include<list>
using namespace std;
int main()
{
   
    std::list<char> li={'+','-','*','@'};
    std::cout <<"back() is :"<< li.back() << std::endl;
    return 0;
}

Output:

Output

back() is : @

In this example, back function returns the last element of the list i.e @.

Input Required

This code uses input(). Please provide values below: