C++ List back()

Last Updated : 18 May 2026

List back() Function

In C++, the list::back() function is used to access the last element of the list container directly. It returns a reference to the last element, which means the returned value can also be modified. This function is useful when we need quick access to the last element of a list without using iterators.

Difference Between back() and end() Function

In C++, both back() and end() are related to the last element of a list, but they work differently. The back() function directly returns a reference to the last element, while the end() function returns an iterator pointing to the position after the last element of the list.

Syntax

It has the following syntax.

Parameters

It does not contain any parameter.

Return value

It returns the direct reference of the last element.

Examples of the List back() Function

Here, we are going to discuss several examples to demonstrate the List back() Function Function.

Example 1: Access the Last Element of a List

This example demonstrates how to use the back() function to access the last element of a list.

Output:

back() is : @

Explanation:

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

Example 2: Modify the Last Element Using back()

This example demonstrates how to use the back() function to modify the last element of a list directly.

Output:

Before modification: 10 20 30 40
After modification: 10 20 30 100
Next Topicswap() Function