C++ std::list push_back() Function

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

Definition

In C++, std::list::push_back() adds a new element at the end of a std::list. The list creates one new node, stores your value in it, and links it after the old last node.

The call is fast whatever the size of the list, and no other element is moved. 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 push_back with the value to add. The value is copied, or moved when you pass a temporary.

C++
l.push_back(30);              // copy a value
l.push_back(std::move(text)); // move a string in
CallWhat it does
l.push_back(value)Copies value into a new last node. size() grows by one.
l.push_back(std::move(value))Moves value into the new node. Use it for large objects you no longer need.

push_back() needs a ready-made object. If you want the list to build the object in place from constructor arguments, use emplace_back().

Library declarations
C++
void push_back(const T& value);
void push_back(T&& value);   // since C++11

Parameters

ParameterDescription
valueRequired. The element to add. It is copied into the new node, or moved if you pass an rvalue.

Return Value

Return ValueDescription
Nothingpush_back() returns void. Use back() if you want to read the new last element.

C++ Examples

Example 1 — push_back() in a Loop

Start with an empty list and add three numbers one by one.

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

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

    for (int i = 1; i <= 3; ++i) {
        l.push_back(i * 10);
    }

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

How It Works

  1. The list starts empty, so it has no nodes at all.
  2. Each pass of the loop calls push_back(i * 10). The values 10, 20, and 30 are linked one after the other.
  3. The range-based for loop reads the nodes from first to last, so the order is the same as the order of the calls.

Example 2 — push_back() Copies the Value

The list gets its own copy. Your original variable is not changed.

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

int main() {
    std::list<std::string> l;
    std::string s = "hello";

    l.push_back(s);
    s = "bye";

    std::cout << "list: " << l.back() << '\n';
    std::cout << "variable: " << s << '\n';
    return 0;
}

How It Works

  1. push_back(s) copies the text "hello" into a new node of the list.
  2. Then s = "bye" changes only the variable. The node in the list has its own separate string.
  3. l.back() still shows hello. If you pass std::move(s) instead, the text is moved and no copy is made.

Example 3 — push_back() Keeps Iterators Valid

Keep an iterator to the first element, add more elements, and read through the old iterator.

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

int main() {
    std::list<int> l = {5};
    auto first = l.begin();

    l.push_back(6);
    l.push_back(7);

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

How It Works

  1. first is an iterator that points at the node that holds 5.
  2. Two calls to push_back() link two new nodes after it. The old node is never copied or moved.
  3. So *first still reads 5. With a std::vector, the same code could break if the vector had to grow.

Frequently Asked Questions

It adds a new element at the end of the list. The list creates a new node, stores a copy of your value in it, and links it after the current last node.
Yes. It creates one node and changes two links, so it does not depend on the size of the list. It does allocate memory for the new node each time.
No. Existing iterators, pointers, and references to elements stay valid. Only end() is a new position.
push_back() receives an object that already exists and copies or moves it. emplace_back() receives constructor arguments and builds the object directly inside the new node.
Use push_front(). A std::list supports both ends, but std::vector has no push_front().

Next: push_front()

Add an element at the start of the list with std::list.

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

12 people found this page helpful