C++ std::string assign_range() Function

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

Definition

In C++, std::string::assign_range() replaces the string with every character from a range (C++23).

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 a range of characters.

C++
s.assign_range(vec);
s.assign_range(std::string_view{"abc"});
CallWhat it does
s.assign_range(rg)Replace contents with characters from rg.

On older C++, assign from iterators: s.assign(v.begin(), v.end()).

Library declarations
C++
template<class R>
constexpr void assign_range(R&& rg); // C++23

Parameters

ParameterDescription
rgRange of char-convertible elements.

Return Value

Return ValueDescription
voidNo return value.

C++ Examples

Example 1 — Assign Vector Chars

Replace with vector contents.

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

int main() {
    std::string s = "old";
    std::vector<char> v = {'n', 'e', 'w'};
    s.assign_range(v);
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Previous text is discarded.
  2. Each vector char becomes part of the string.
  3. Result is "new".

Example 2 — Assign string_view

Assign from a view.

C++
#include <iostream>
#include <string>
#include <string_view>

int main() {
    std::string s = "x";
    s.assign_range(std::string_view{"OK"});
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. The view provides two characters.
  2. assign_range copies them into s.
  3. Printed value is "OK".

Example 3 — Assign Empty

Assigning an empty range clears the string.

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

int main() {
    std::string s = "gone";
    std::vector<char> v;
    s.assign_range(v);
    std::cout << std::boolalpha << s.empty() << '\n';
    return 0;
}

How It Works

  1. The range has no elements.
  2. The string becomes empty.
  3. empty() reports true.

Frequently Asked Questions

C++23 member that replaces the string with a range of characters.
C++23.
s.assign(r.begin(), r.end()) on older modes.
No. It replaces, like assign.
Yes. Then the string becomes empty.

Next: at()

Keep going through the string functions. Read or change a character by index; throws if out of range.

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

10 people found this page helpful