GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::basic_string::insert - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. string
  3. basic_string
  4. insert

最終更新日時: 2026年10月03日 22時31分04秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<string>

std::basic_string::insert

basic_string&
  insert(size_type pos1, const basic_string& str);    // (1) C++98
constexpr basic_string&
  insert(size_type pos1, const basic_string& str);    // (1) C++20

basic_string&
  insert(size_type pos1, const basic_string& str,
         size_type pos2, size_type n);                // (2) C++98
basic_string&
  insert(size_type pos1, const basic_string& str,
         size_type pos2, size_type n = npos);         // (2) C++14
constexpr basic_string&
  insert(size_type pos1, const basic_string& str,
         size_type pos2, size_type n = npos);         // (2) C++20

basic_string&
  insert(size_type pos, const charT* s, size_type n); // (3) C++98
constexpr basic_string&
  insert(size_type pos, const charT* s, size_type n); // (3) C++20

basic_string&
  insert(size_type pos, const charT* s);              // (4) C++98
constexpr basic_string&
  insert(size_type pos, const charT* s);              // (4) C++20

basic_string&
  insert(size_type pos, size_type n, charT c);        // (5) C++98
constexpr basic_string&
  insert(size_type pos, size_type n, charT c);        // (5) C++20

iterator insert(iterator p, charT c);                 // (6) C++98
iterator insert(const_iterator p, charT c);           // (6) C++11
constexpr iterator insert(const_iterator p, charT c); // (6) C++20

void insert(iterator p, size_type n, charT c);                     // (7) C++98
iterator insert(const_iterator p, size_type n, charT c);           // (7) C++11
constexpr iterator insert(const_iterator p, size_type n, charT c); // (7) C++20

template<class InputIterator>
void
  insert(iterator p,
         InputIterator first, InputIterator last); // (8) C++98
template<class InputIterator>
iterator
  insert(const_iterator p,
         InputIterator first, InputIterator last); // (8) C++11
template<class InputIterator>
constexpr iterator
  insert(const_iterator p,
         InputIterator first, InputIterator last); // (8) C++20

iterator
  insert(const_iterator p, initializer_list<charT>); // (9) C++11
constexpr iterator
  insert(const_iterator p, initializer_list<charT>); // (9) C++20

// string_viewを引数に取るオーバーロード
template<class T>
basic_string&
  insert(size_type pos1,
         const T& t);    // (10) C++17
template<class T>
constexpr basic_string&
  insert(size_type pos1,
         const T& t);    // (10) C++20

template<class T>
basic_string&
  insert(size_type pos1,
         const T& t,
         size_type pos2,
         size_type n = npos); // (11) C++17
template<class T>
constexpr basic_string&
  insert(size_type pos1,
         const T& t,
         size_type pos2,
         size_type n = npos); // (11) C++20

概要

文字/文字列を挿入する。

テンプレートパラメータ制約

  • (10), (11) : 以下の両方を満たしていること
    • is_convertible_v<const T&, basic_string_view<charT, traits>> == true
    • is_convertible_v<const T&, const charT*> == false

要件

  • (1) : pos <= size()
  • (2) : pos1 <= size()およびpos2 <= str.size()
  • (3) : 文字配列へのポインタsが指す配列が少なくてもn要素あり、pos <= size()であること。
  • (4) : pos <= size()、および文字配列へのポインタsが、少なくてもtraits::length(s) + 1個の要素を指す配列を指していること。
  • (6) : イテレータpが、*thisに対して有効であること。
  • (7) : イテレータpが、*thisに対して有効であること。
  • (8) : イテレータpが、*thisに対して有効であること。[first, last)が有効な範囲であること。

効果

  • (1) : insert(pos, str.data(), str.size())
  • (2) :
    • str.size() - pos2とnのうち小さい方をrlenとする。n == npos の場合は、 str.size() - pos2 が使用される。
    • insert(pos1, str.data() + pos2, rlen)を呼び出す。
  • (3) : *thisのpos番目に、文字配列sの先頭n文字を挿入する。
  • (4) : insert(pos, s, traits::length(s))と等価の効果を持つ。
  • (5) : insert(pos, basic_string(n, c))と等価の効果を持つ。
  • (6) : イテレータpが指す要素の前に、文字cのコピーを挿入する。
  • (7) : イテレータpが指す要素の前に、文字cのコピーをn個挿入する。
  • (8) : insert(p - begin(), basic_string(first, last))と等価の効果を持つ。
  • (9) : insert(p, il.begin(), il.end())
  • (10) : 以下と等価。

    basic_string_view<charT, traits> sv = t;
    return insert(pos1, sv.data(), sv.size());
    

  • (11) : 以下と等価。

    basic_string_view<charT, traits> sv = t;
    return insert(pos1, sv.substr(pos2, n));
    

戻り値

  • (1) : *this
  • (2) : *this
  • (3) : *this
  • (4) : *this
  • (5) : *this
  • (6) : 挿入された文字を指すイテレータを返す。
  • (7) : 挿入された最初の文字を指すイテレータを返す。n == 0ならpを返す。
  • (8) : 挿入された最初の文字を指すイテレータを返す。first == lastならpを返す。
  • (10) : *this
  • (11) : *this

例外

  • (1) : pos > size()の場合、out_of_range例外を送出する。
  • (2) : pos1 > size()もしくはpos2 > str.size()の場合、out_of_range例外を送出する。
  • (3) : pos > size()の場合、out_of_range例外を送出する。また、size() + n > max_size()の場合にはlength_error例外を送出する。
  • (11) : pos1 > size()もしくはpos2 > sv.size()の場合、out_of_range例外を送出する。

例

#include <iostream>
#include <string>
#include <string_view>

int main()
{
  // (1) 指定位置に文字列を挿入する
  {
    std::string s1 = "aaaaa";
    std::string s2 = "bbbbb";

    s1.insert(2, s2);

    std::cout << "(1) : " << s1 << std::endl;
  }

  // (2) 指定位置に、部分文字列を挿入する
  {
    std::string s1 = "aaaaa";
    std::string s2 = "12345";

    // s2.substr(2, 3)を挿入する
    s1.insert(2, s2, 2, 3);

    std::cout << "(2) : " << s1 << std::endl;
  }

  // (3) 指定位置に、文字配列の先頭N文字を挿入する
  {
    std::string s = "aaaaa";

    s.insert(2, "bbbbb", 3);

    std::cout << "(3) : " << s << std::endl;
  }

  // (4) 指定位置に文字配列を挿入する
  {
    std::string s = "aaaaa";

    s.insert(2, "bbbbb");

    std::cout << "(4) : " << s << std::endl;
  }

  // (5) 指定位置に、N個の文字を挿入する
  {
    std::string s = "aaaaa";

    s.insert(2, 3, 'b');

    std::cout << "(5) : " << s << std::endl;
  }

  // (6) 指定したイテレータが指す要素の前に、文字を挿入する
  {
    std::string s = "aaaaa";

    s.insert(s.begin(), 'b');

    std::cout << "(6) : " << s << std::endl;
  }

  // (7) 指定したイテレータが指す要素の前に、N個の文字を挿入する
  {
    std::string s = "aaaaa";

    s.insert(s.begin(), 3, 'b');

    std::cout << "(7) : " << s << std::endl;
  }

  // (8) 指定したイテレータが指す要素の前に、文字の範囲を挿入する
  {
    std::string s1 = "aaaaa";
    std::string s2 = "bbbbb";

    s1.insert(s1.begin(), s2.begin(), s2.end());

    std::cout << "(8) : " << s1 << std::endl;
  }

  // (9) 指定したイテレータが指す要素の前に、文字の初期化子リストを挿入する
  {
    std::string s = "aaaaa";

    s.insert(s.begin(), {'b', 'b', 'b', 'b', 'b'});

    std::cout << "(9) : " << s << std::endl;
  }

  // (10) 指定位置にbasic_string_viewが参照する文字列範囲を挿入する
  {
    std::string s1 = "aaaaa";
    std::string_view sv2 = std::string_view{"CCCbbbbbDDD"}.substr(3, 5);

    s1.insert(2, sv2);

    std::cout << "(10) : " << s1 << std::endl;
  }

  // (11) 指定位置に、basic_string_viewの指定された範囲を挿入する
  {
    std::string s1 = "aaaaa";
    std::string_view sv2 = "CCCbbbbbDDD";

    s1.insert(2, sv2, 3, 5);

    std::cout << "(11) : " << s1 << std::endl;
  }
}

出力

(1) : aabbbbbaaa
(2) : aa345aaa
(3) : aabbbaaa
(4) : aabbbbbaaa
(5) : aabbbaaa
(6) : baaaaa
(7) : bbbaaaaa
(8) : bbbbbaaaaa
(9) : bbbbbaaaaa
(10) : aabbbbbaaa
(11) : aabbbbbaaa

参照

  • LWG Issue 180. Container member iterator arguments constness has unintended consequences
  • LWG ISsue 2268. Setting a default argument in the declaration of a member function assign of std::basic_string
    • C++14から(2)のオーバーロードに、n = nposのデフォルト引数を追加。
  • P0254R2 Integrating std::string_view and std::string
  • LWG Issue 2758. std::string{}.assign("ABCDE", 0, 1) is ambiguous
  • LWG Issue 2946. LWG 2758's resolution missed further corrections
    • 意図しない暗黙変換防止のためにstring_viewを受けるオーバーロード(10), (11)の引数型をconst T&に変更
  • P0980R1 Making std::string constexpr
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.