C++ std::string insert_range() Function

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

Definition

In C++, std::string::insert_range() inserts every character from a range before an iterator position (C++23).

Include <string> and enable 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 an iterator and a range.

C++
s.insert_range(s.begin() + 1, vec);
CallWhat it does
s.insert_range(pos, rg)Insert all of rg before iterator pos.

On older standards, insert in a loop or use iterator-pair insert overloads.

Library declarations
C++
template<class R>
constexpr iterator insert_range(const_iterator pos, R&& rg); // C++23

Parameters

ParameterDescription
posIterator before which to insert.
rgRange of characters.

Return Value

Return ValueDescription
iteratorIterator to the first inserted character (or pos if the range was empty).

C++ Examples

Example 1 — Insert Range in Middle

Insert chars from a vector at index 1.

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

int main() {
    std::string s = "AC";
    std::vector<char> v = {'B'};
    s.insert_range(s.begin() + 1, v);
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Iterator at offset 1 points before 'C'.
  2. The range inserts 'B' there.
  3. Result is "ABC".

Example 2 — Range at begin()

Insert a prefix from a string_view.

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

int main() {
    std::string s = "Fun";
    s.insert_range(s.begin(), std::string_view{"CodeTo"});
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Insertion happens at begin().
  2. All view characters are copied in order.
  3. Original "Fun" shifts right.

Example 3 — Insert Empty Range

Empty range leaves text unchanged.

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

int main() {
    std::string s = "keep";
    std::vector<char> v;
    s.insert_range(s.end(), v);
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. The vector is empty.
  2. Nothing is inserted.
  3. The string stays "keep".

Frequently Asked Questions

C++23 API to insert a whole range of characters at an iterator.
C++23.
An iterator to the first inserted character.
Yes.
Use insert with iterators or a loop.

Next: length()

Keep going through the string functions. Same as size(): how many characters are in the string.

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