C++ std::deque assign() Function

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

Definition

In C++, std::deque::assign() replaces the whole content of a std::deque. It first removes every old element, then stores the new ones you describe.

You can describe the new elements in three ways: a count and a value, a pair of iterators, or a {...} list. Include <deque> to use it.

This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.

Syntax

Call assign on the deque you already have. Pick the form that matches how your new data is stored.

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

d = other; also replaces the content, but it needs a whole deque. assign() works with ranges and lists too.

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 deque should hold afterwards.
valueRequired for the first form. The value copied into every new element.
first, lastRequired for the second form. The range to copy. last points one place after the final element. They must not point into the same deque.
ilistRequired for the third form. A braced list such as {1, 2, 3}.

Return Value

Return ValueDescription
Nothingassign() returns void. The deque 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 <deque>
#include <iostream>

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

    d.assign(4, 9);

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

How It Works

  1. The deque starts with the three values 1 2 3.
  2. assign(4, 9) deletes those three values and stores four copies of 9.
  3. The size is now 4, even though the old deque was shorter. The new count wins.

Example 2 — assign() from an Iterator Range

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

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

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

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

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

How It Works

  1. src.begin() + 1 points at 20, the second item of the vector.
  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.

Example 3 — assign() with a Braced List

Use a {...} list when you know the new values by heart.

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

int main() {
    std::deque<std::string> names = {"Ann", "Bob", "Cy"};

    names.assign({"Zoe", "Max"});

    std::cout << "count: " << names.size() << '\n';
    for (const std::string& s : names) {
        std::cout << s << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The deque of strings starts with three names.
  2. assign({"Zoe", "Max"}) replaces all three with just two names.
  3. The loop uses const std::string&, so each name is read without making a copy.

Frequently Asked Questions

It removes every element that the deque 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 deque into another deque. assign() is more flexible: it can fill from a count, from any iterator range (even a vector), or from a braced list.
No. All old elements 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 deque is undefined behavior because assign() destroys the old elements first.
Yes. std::vector::assign() has the same three forms. The deque version works in the same way, but it stores data in several blocks instead of one.

Next: assign_range()

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

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