C++ std::vector assign_range() Function

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

Definition

In C++, std::vector::assign_range() replaces the content of a std::vector with the elements of a range. A range is anything you can loop over: a vector, an array, a list, or a lazy view such as std::views::iota.

It was added in C++23. Unlike assign(first, last), you pass the whole range as one argument, so there is no need to write begin() and end(). 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_range with one range. Compile with a C++23 compiler setting, for example -std=c++23.

C++
v.assign_range(v);                    // v is a vector
v.assign_range(std::views::iota(1, 6)); // 1 2 3 4 5
CallWhat it does
v.assign_range(v)Removes the old elements and copies every element of v into the vector.
v.assign_range(std::views::iota(1, 6))Fills the vector from a view that produces 1 2 3 4 5.

If your compiler is older than C++23, use assign(first, last) instead. It does the same job.

Library declarations
C++
template<container-compatible-range<T> R>
void assign_range(R&& rg);   // since C++23

Parameters

ParameterDescription
rgRequired. A range whose element type can be converted to T. It must not be a range that refers to this same vector.

Return Value

Return ValueDescription
Nothingassign_range() returns void. The old elements are replaced by copies of the range elements.

C++ Examples

Example 1 — assign_range() from a Vector

Replace five old values with the three values of a vector. Compile with -std=c++23.

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

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

    d.assign_range(v);

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

How It Works

  1. The vector begins with five copies of 1.
  2. assign_range(v) takes the whole vector as one argument. No begin() or end() is needed.
  3. The old five elements are removed, so the size becomes 3, the size of the vector.

Example 2 — assign_range() with std::views::iota

A range does not need to be stored anywhere. A view can create numbers on demand.

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

int main() {
    std::vector<int> v;

    v.assign_range(std::views::iota(1, 6));

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

How It Works

  1. std::views::iota(1, 6) describes the numbers from 1 up to, but not including, 6.
  2. assign_range() walks through that view and stores each number in the vector.
  3. The vector was empty at the start, so the result is just the five generated numbers.

Example 3 — assign_range() from a std::array

Show that the old content is really gone after the call.

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

int main() {
    std::array<int, 3> src = {8, 7, 6};
    std::vector<int> v = {100, 200};

    v.assign_range(src);

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

How It Works

  1. The vector first holds 100 and 200.
  2. After assign_range(src) the vector holds 8 7 6, in the same order as the array.
  3. front() and back() show the first and last copied values. 100 and 200 do not appear anywhere.

Frequently Asked Questions

It replaces the content of the vector with the elements of a range. A range can be a vector, an array, a list, or a view such as std::views::iota.
C++23. With older compilers or older -std settings it does not exist, so use assign(first, last) instead.
assign(first, last) needs two iterators. assign_range(rg) needs one range object, which is shorter and works with views. Both remove the old elements first.
No. It destroys every old element and then stores copies of the elements from the range.
In C++23 use append_range(rg) to add at the back, prepend_range(rg) to add at the front, or insert_range(pos, rg) for any place. In older versions use insert(pos, first, last).

Next: at()

Read or change one element by index, with a bounds check with std::vector.

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

9 people found this page helpful