Home C++ Vectors Functions append_range() C++ std::vector append_range() Function 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).
v.append_range(other);
v.append_range(std::views::iota(1, 4)); Call What 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 template<container-compatible-range<T> R>
void append_range(R&& rg); // since C++23 Parameters Parameter Description rgRequired. A range whose elements can be converted to T. It must not be a range into this same vector.
Return Value Return Value Description Nothing append_range() returns void. The size grows by the number of elements appended.
Important
Like other inserts at the end, append_range() may reallocate. If it reallocates, all iterators, references, and pointers become invalid. Call reserve() first when you know the final size.
C++ Examples 1. Append a vector 2. Append a view 3. Reserve then append Example 1 — Append Another Vector Add every element of one vector to the end of another.
#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 Vector v starts as 1 2. append_range(extra) copies 3 4 5 to the end.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.
#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 std::views::iota(11, 14) produces 11 12 13.append_range walks that view and pushes each value onto v.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.
#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;
} size=4 capacity=4
1 2 3 4 How It Works reserve(v.size() + more.size()) prepares exact room for the append.append_range then copies without needing another allocation.Size becomes 4. Capacity is at least 4. Frequently Asked Questions What does std::vector::append_range() do? It appends every element of a range to the end of the vector. Existing elements stay.
Which C++ version added append_range()? C++23. Older code can use insert(v.end(), first, last) instead.
How is append_range() different from assign_range()? assign_range() replaces the whole content. append_range() only adds at the end.
Does append_range() invalidate iterators? It may reallocate. If capacity was large enough, only iterators at/after the end may be affected; if it reallocates, everything is invalidated.
Does std::deque have append_range() too? Yes, starting in C++23. The idea is the same; a deque does not expose capacity() or reserve().
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion