C++ std::vector append_range() Function

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

Definition

In C++, std::vector::append_range() copies (or moves) every element of a range to the end of a std::vector. Old elements stay.

It was added in C++23. Prefer it over a hand-written loop of push_back when the source is already a range. Include <vector> and compile with C++23.

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

Pass one range. Compile with -std=c++23 (or newer).

C++
v.append_range(other);
v.append_range(std::views::iota(1, 4));
CallWhat it does
v.append_range(other)Appends every element of other after the current last element.
v.append_range(std::views::iota(1, 4))Appends 1 2 3 from a view.

If your compiler is older than C++23, use v.insert(v.end(), first, last) instead.

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

Parameters

ParameterDescription
rgRequired. A range whose elements can be converted to T. It must not be a range into this same vector.

Return Value

Return ValueDescription
Nothingappend_range() returns void. The size grows by the number of elements appended.

C++ Examples

Example 1 — Append Another Vector

Add every element of one vector to the end of another.

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

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

    v.append_range(extra);

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

How It Works

  1. Vector v starts as 1 2.
  2. append_range(extra) copies 3 4 5 to the end.
  3. The old front values stay. extra is not changed.

Example 2 — Append a views::iota Range

Append a lazy sequence without building a temporary vector first.

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

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

    v.append_range(std::views::iota(11, 14));

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

How It Works

  1. std::views::iota(11, 14) produces 11 12 13.
  2. append_range walks that view and pushes each value onto v.
  3. The result is 10 11 12 13. You needed C++23 ranges support for this.

Example 3 — Reserve Before append_range()

Avoid reallocation when you know how many elements will be added.

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

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

    v.reserve(v.size() + more.size());
    v.append_range(more);

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

How It Works

  1. reserve(v.size() + more.size()) prepares exact room for the append.
  2. append_range then copies without needing another allocation.
  3. Size becomes 4. Capacity is at least 4.

Frequently Asked Questions

It appends every element of a range to the end of the vector. Existing elements stay.
C++23. Older code can use insert(v.end(), first, last) instead.
assign_range() replaces the whole content. append_range() only adds at the end.
It may reallocate. If capacity was large enough, only iterators at/after the end may be affected; if it reallocates, everything is invalidated.
Yes, starting in C++23. The idea is the same; a deque does not expose capacity() or reserve().

Next: assign()

Replace all elements with new ones with std::vector.

assign() 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