C++ std::list assign() Function

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

Definition

In C++, std::list::assign() replaces the whole content of a std::list. It first erases every old element, then builds new nodes for the elements you describe.

You can describe the new elements in three ways: a count and a value, a pair of iterators, or a {...} list. 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 assign on the list you already have. Pick the form that matches how your new data is stored.

C++
l.assign(4, 9);                    // four copies of 9
l.assign(src.begin(), src.end());  // copy from another container
l.assign({1, 2, 3});               // from a braced list
CallWhat it does
l.assign(4, 9)The list now holds four elements, all equal to 9.
l.assign(first, last)Copies every element of the iterator range [first, last). The source can be a vector, deque, array, or another list.
l.assign({1, 2, 3})Replaces the content with the values in the braces.

l = other; also replaces the content, but it needs a whole list. assign() also accepts counts, ranges of other containers, and braced lists.

Library declarations
C++
void assign(size_type count, const T& value);

template<class InputIt>
void assign(InputIt first, InputIt last);

void assign(std::initializer_list<T> ilist);

Parameters

ParameterDescription
countRequired for the first form. How many elements the list should hold afterwards.
valueRequired for the first form. The value copied into every new node.
first, lastRequired for the second form. The range to copy. last points one place after the final element. The range must not come from the same list.
ilistRequired for the third form. A braced list such as {1, 2, 3}.

Return Value

Return ValueDescription
Nothingassign() returns void. The list itself is changed.

C++ Examples

Example 1 — assign() with a Count and Value

Replace three old numbers with four copies of the same number.

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

int main() {
    std::list<int> l = {1, 2, 3};

    l.assign(4, 9);

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

How It Works

  1. The list starts as three linked nodes holding 1 2 3.
  2. assign(4, 9) unlinks and destroys those three nodes, then creates four new nodes that each hold 9.
  3. The size is 4. The new count wins, even though the old list was shorter.

Example 2 — assign() from an Iterator Range

Copy part of a vector into a list. The second iterator points one place after the last element you want.

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

int main() {
    std::vector<int> src = {10, 20, 30, 40, 50};
    std::list<int> l = {1, 2};

    l.assign(src.begin() + 1, src.begin() + 4);

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

How It Works

  1. A vector has random access, so src.begin() + 1 is allowed and points at 20.
  2. src.begin() + 4 points at 50. The range stops before it, so only 20 30 40 are copied.
  3. The old values 1 2 are gone. The vector src is not changed. Note that you could not write l.begin() + 1 on the list itself.

Example 3 — assign() and Then push_back()

After assign() the list is a normal list again, so you can keep adding to it.

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

int main() {
    std::list<int> l = {100, 200, 300};

    l.assign({7, 8});
    l.push_back(9);

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

How It Works

  1. assign({7, 8}) drops 100 200 300 and stores just two nodes, 7 and 8.
  2. push_back(9) links a third node at the end. The first two nodes stay where they are.
  3. front() reads 7, back() reads 9, and size() counts three elements.

Frequently Asked Questions

It erases every element that the list holds and then stores new elements. The new elements come from a count and value, a pair of iterators, or an initializer list.
operator= copies a whole list into another list. assign() is more flexible: it can fill from a count, from an iterator range of any container, or from a braced list.
No. All old nodes are destroyed first. If you want to keep them, use push_back(), push_front(), or insert().
No. The iterator range must come from a different container. Pointing into the same list is undefined behavior because assign() destroys the old nodes first.
Yes. std::vector::assign() has the same three forms. A list builds one new node for every element, while a vector may reuse its existing memory block.

Next: assign_range()

Replace all elements with the items of a range (C++23) with std::list.

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

7 people found this page helpful