C++ std::vector assign() Function

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

Definition

In C++, std::vector::assign() replaces the whole content of a std::vector. 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 <vector> to use it.

This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.

Syntax

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

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

d = other; also replaces the content, but it needs a whole vector. 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 vector 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 vector.
ilistRequired for the third form. A braced list such as {1, 2, 3}.

Return Value

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

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

    v.assign(4, 9);

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

How It Works

  1. The vector 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 vector was shorter. The new count wins.

Example 2 — assign() from an Iterator Range

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

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

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

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

    for (int n : v) {
        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 <vector>
#include <iostream>
#include <string>

int main() {
    std::vector<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 vector 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 vector 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 vector into another vector. 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() or insert().
No. The iterator range must come from a different container. Pointing into the same vector is undefined behavior because assign() destroys the old elements first.
Yes. std::vector::assign() has the same three forms. The deque version works in a similar way, but a deque stores data in several blocks while a vector uses one contiguous block.

Next: assign_range()

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

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.

8 people found this page helpful