C++ std::list back() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::list

Definition

In C++, std::list::back() gives you a reference to the last element of a std::list. You can read it, or assign to it to change the last element.

A list is doubly linked, so reaching the last element is as quick as reaching the first one. Include <list> to use it.

This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.

Syntax

Call back with empty brackets. The list must not be empty.

C++
int z = l.back();      // read the last element
l.back() = 0;          // change the last element
CallWhat it does
l.back()Returns a reference to the last element. On a const list it returns a const reference.

Do not use *l.end() to get the last element. end() points one place after the last element. Use *std::prev(l.end()) or just back().

Library declarations
C++
reference back();
const_reference back() const;

Parameters

ParameterDescription
Noneback() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
referenceA reference to the last element. For a const list, a const_reference.

C++ Examples

Example 1 — back() Reads the Last Element

Print the last element of a list, and compare it with *std::prev(l.end()).

C++
#include <iostream>
#include <iterator>
#include <list>

int main() {
    std::list<int> l = {10, 20, 30};

    std::cout << "back: " << l.back() << '\n';
    std::cout << "prev(end): " << *std::prev(l.end()) << '\n';
    return 0;
}

How It Works

  1. The last node of the list holds 30.
  2. l.back() returns a reference to that element, so it prints 30.
  3. l.end() is one place after the last element, so std::prev(l.end()) steps back to the last node. It gives the same value as back().

Example 2 — back() Lets You Change the Element

Assign to the reference that back() returns.

C++
#include <iostream>
#include <list>
#include <string>

int main() {
    std::list<std::string> l = {"red", "green", "blue"};

    l.back() = "black";
    l.back() += "!";

    for (const std::string& s : l) {
        std::cout << s << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. l.back() is a reference to the string "blue" inside the last node.
  2. l.back() = "black" replaces that text, and += "!" then appends an exclamation mark to the same string.
  3. The first two strings are not changed, so the loop prints red green black!.

Example 3 — front() and back() with One Element

In a list with a single element, the first and the last element are the same node.

C++
#include <iostream>
#include <list>

int main() {
    std::list<int> l = {42};

    std::cout << std::boolalpha;
    std::cout << "same node: " << (&l.front() == &l.back()) << '\n';

    l.push_back(7);
    std::cout << "front: " << l.front() << ", back: " << l.back() << '\n';
    return 0;
}

How It Works

  1. With only one node, front() and back() both return a reference to it. Taking the address with & shows that it is the same object.
  2. push_back(7) links a second node after it. The old node is still the first one.
  3. Now the two functions return different elements: front() is 42 and back() is 7.

Frequently Asked Questions

A reference to the last element of the list. You can read it or assign a new value to it.
The behavior is undefined. Always check empty() first.
end() points one place after the last element, so dereferencing it is undefined behavior. Use back(), or *std::prev(l.end()).
No. back() only gives access. To remove the last element, call pop_back() after reading it.
Yes. A std::list is doubly linked and remembers its last node, so back() takes constant time.

Next: begin()

Get an iterator to the first element with std::list.

begin() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful