Home C++ Strings Functions append_range() C++ std::string append_range() Function Definition In C++, std::string::append_range() appends every character from a range (C++23).
Useful with containers or views of char. Include <string> and compile as C++23.
This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.
Syntax Pass any suitable range of characters.
s.append_range(vec);
s.append_range(std::string_view{"xyz"}); Call What it does s.append_range(rg)Append each character in range rg.
If your compiler is older than C++23, use a loop with push_back or append instead.
Library declarations template<class R>
constexpr void append_range(R&& rg); // C++23 Parameters Parameter Description rgA range whose elements are convertible to char.
Return Value Return Value Description voidNo return value.
C++ Examples 1. From vector 2. From string_view 3. Empty range Example 1 — Append a Vector of Char Append chars stored in a vector.
#include <iostream>
#include <string>
#include <vector>
int main() {
std::string s = "A";
std::vector<char> v = {'B', 'C'};
s.append_range(v);
std::cout << s << '\n';
return 0;
} How It Works The string starts as "A". append_range copies each vector element.Result is "ABC". Example 2 — Append a string_view Append a view without making a temporary std::string.
#include <iostream>
#include <string>
#include <string_view>
int main() {
std::string s = "Hi ";
std::string_view sv = "there";
s.append_range(sv);
std::cout << s << '\n';
return 0;
} How It Works string_view is a non-owning range of chars.append_range copies those chars onto s. Printed text is "Hi there". Example 3 — Append Nothing Appending an empty range leaves the string unchanged.
#include <iostream>
#include <string>
#include <vector>
int main() {
std::string s = "same";
std::vector<char> v;
s.append_range(v);
std::cout << s << '\n';
return 0;
} How It Works The vector has no elements. append_range adds nothing. The string stays "same". Frequently Asked Questions What is append_range? A C++23 member that appends every character from a range.
Which standard added it? C++23.
Can I append a string_view? Yes, string_view is a range of char.
What if my compiler lacks C++23? Loop with push_back or use append overloads.
Does it return *this? The common form returns void; check your library signature.
Next: assign() Keep going through the string functions. Replace the entire contents with new characters.
assign() tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
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.
8 people found this page helpful
Helpful Share Copy link Suggestion