C++ std::list front() Function

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

Definition

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

Because a list has no l[0], front() and back() are the main ways to reach the ends. 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 front with empty brackets. The list must not be empty.

C++
int a = l.front();    // read the first element
l.front() = 99;       // change the first element
CallWhat it does
l.front()Returns a reference to the first element. On a const list it returns a const reference.

front() gives the element itself. begin() gives an iterator to it. The two are linked: l.front() equals *l.begin().

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

Parameters

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

Return Value

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

C++ Examples

Example 1 — front() Reads the First Element

Print the first element of a list, and show that it is the same as *l.begin().

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

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

    std::cout << "front: " << l.front() << '\n';
    std::cout << "*begin: " << *l.begin() << '\n';
    return 0;
}

How It Works

  1. The list has three elements. The first node holds 10.
  2. l.front() returns a reference to that element, so printing it shows 10.
  3. *l.begin() follows the iterator to the same node, so both lines show the same value.

Example 2 — front() Lets You Change the Element

Because front() returns a reference, you can assign to it.

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

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

    l.front() = 99;
    l.front() += 1;

    for (int n : l) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. l.front() = 99 writes 99 into the first node, because front() is a reference and not a copy.
  2. l.front() += 1 adds one to the same element, so it becomes 100.
  3. The other two nodes are not touched, so the loop prints 100 20 30.

Example 3 — front() and an Empty List

Write a helper function that checks empty() before it calls front().

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

void show(const std::list<int>& l) {
    if (l.empty()) {
        std::cout << "list is empty\n";
    } else {
        std::cout << "front: " << l.front() << '\n';
    }
}

int main() {
    std::list<int> none;
    std::list<int> some = {4, 5};

    show(none);
    show(some);
    return 0;
}

How It Works

  1. The function takes the list by const reference, so no copy is made and the list cannot be changed.
  2. For the empty list, empty() is true, so front() is never called. That avoids undefined behavior.
  3. For {4, 5} the check fails, so front() runs safely and prints 4.

Frequently Asked Questions

A reference to the first element of the list. You can read it or assign a new value to it.
The behavior is undefined. The program may crash or show garbage. Always check empty() first.
front() returns the first element itself (a reference). begin() returns an iterator that points at the first element. On an empty list begin() is still safe to compare, but dereferencing it is not.
A list has no operator[] or at(). Move an iterator with std::next(l.begin(), i) and dereference it. This walks node by node, so it is slow for big i.
Yes. It takes constant time because the list always knows its first node.

Next: get_allocator()

Get a copy of the allocator the list uses with std::list.

get_allocator() 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.

13 people found this page helpful