English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and examples of List back()

C++ List (List)

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

Difference between back() and end() functions

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

Syntax

reference back();

Parameter

It does not contain any parameters.

Return value

It returns a direct reference to the last element.

Example

Let's see a simple example

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

Output:

back() is: @

In this example, the back() function returns the direct reference to the last element of the list, that is, @.

C++ List (List)