C++ std::string append_range() Function

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

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.

C++
s.append_range(vec);
s.append_range(std::string_view{"xyz"});
CallWhat 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
C++
template<class R>
constexpr void append_range(R&& rg); // C++23

Parameters

ParameterDescription
rgA range whose elements are convertible to char.

Return Value

Return ValueDescription
voidNo return value.

C++ Examples

Example 1 — Append a Vector of Char

Append chars stored in a vector.

C++
#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

  1. The string starts as "A".
  2. append_range copies each vector element.
  3. Result is "ABC".

Example 2 — Append a string_view

Append a view without making a temporary std::string.

C++
#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

  1. string_view is a non-owning range of chars.
  2. append_range copies those chars onto s.
  3. Printed text is "Hi there".

Example 3 — Append Nothing

Appending an empty range leaves the string unchanged.

C++
#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

  1. The vector has no elements.
  2. append_range adds nothing.
  3. The string stays "same".

Frequently Asked Questions

A C++23 member that appends every character from a range.
C++23.
Yes, string_view is a range of char.
Loop with push_back or use append overloads.
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.

Your daily dose of tech, straight to your feed.

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