名前空間
変種
操作

std::basic_string<CharT,Traits,Allocator>::insert

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
basic_string& insert( size_type index, size_type count, CharT ch );
(1) (C++20 以降 constexpr)
basic_string& insert( size_type index, const CharT* s );
(2) (C++20 以降 constexpr)
basic_string& insert( size_type index, const CharT* s, size_type count );
(3) (C++20 以降 constexpr)
basic_string& insert( size_type index, const basic_string& str );
(4) (C++20 以降 constexpr)
(5)
basic_string& insert( size_type index, const basic_string& str,
                      size_type s_index, size_type count );
(C++14まで)
basic_string& insert( size_type index, const basic_string& str,
                      size_type s_index, size_type count = npos );
(C++14以降)
(C++20 以降 constexpr)
(6)
iterator insert( iterator pos, CharT ch );
(C++11まで)
iterator insert( const_iterator pos, CharT ch );
(C++11以降)
(C++20 以降 constexpr)
(7)
void insert( iterator pos, size_type count, CharT ch );
(C++11まで)
iterator insert( const_iterator pos, size_type count, CharT ch );
(C++11以降)
(C++20 以降 constexpr)
(8)
template< class InputIt >
void insert( iterator pos, InputIt first, InputIt last );
(C++11まで)
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );
(C++11以降)
(C++20 以降 constexpr)
iterator insert( const_iterator pos, std::initializer_list<CharT> ilist );
(9) (C++11以降)
(C++20 以降 constexpr)
template< class StringViewLike >
basic_string& insert( size_type index, const StringViewLike& t );
(10) (C++17以降)
(C++20 以降 constexpr)
template< class StringViewLike >

basic_string& insert( size_type index, const StringViewLike& t,

                      size_type t_index, size_type count = npos );
(11) (C++17以降)
(C++20 以降 constexpr)

文字列に文字を挿入します。

1) 位置 `index` に文字 `ch` を `count` 個挿入します。
2) `index` の位置に、ヌル終端文字列 `s` が指す文字を挿入します。文字列の長さは、最初のヌル文字によって `Traits::length(s)` を使用して決定されます。
3) `index` の位置に、範囲 `[s, s + count)` の文字を挿入します。範囲にはヌル文字を含めることができます。
4) `index` の位置に文字列 `str` を挿入します。
5) `index` の位置に、`str.substr(s_index, count)` によって得られた文字列を挿入します。
6) `pos` が指す文字の前に文字 `ch` を挿入します。
7) `pos` が指す要素(存在する場合)の前に、文字 `ch` を `count` 個挿入します。
8) `pos` が指す要素(存在する場合)の前に、範囲 `[first, last)` の文字を挿入します。これは、`insert(pos - begin(), basic_string(first, last, get_allocator()))` と同様に行われます。

`InputIt` が LegacyInputIterator を満たさない場合、このオーバーロードはオーバーロード解決に参加しません。

(C++11以降)
9) `pos` が指す要素(存在する場合)の前に、初期化子リスト `ilist` の要素を挿入します。
10) `t` を、`std::basic_string_view sv = t;` のように文字列ビューに暗黙的に変換してから、`index` が指す要素(存在する場合)の前に `sv` の要素を挿入します。これは、`insert(index, sv.data(), sv.size())` と同様に行われます。
このオーバーロードは、`std::is_convertible_v>` が `true` であり、`std::is_convertible_v` が `false` である場合にのみ、オーバーロード解決に参加します。
11) `t` を、`std::basic_string_view sv = t;` のように文字列ビューに暗黙的に変換してから、`index` が指す要素(存在する場合)の前に、`sv` のサブビュー `[t_index, t_index + count)` の文字を挿入します。
  • 要求されたサブビューが `sv` の末尾を超える場合、または `count == npos` の場合、結果のサブビューは `[t_index, sv.size())` となります。
  • `t_index > sv.size()`、または `index > size()` の場合、`std::out_of_range` がスローされます。
このオーバーロードは、`std::is_convertible_v>` が `true` であり、`std::is_convertible_v` が `false` である場合にのみ、オーバーロード解決に参加します。

`pos` が `*this` の有効なイテレータでない場合、動作は未定義です。

目次

[編集] パラメータ

index - 挿入するコンテンツの位置
pos - 文字を挿入する前のイテレータ
文字 - 挿入する文字
count - 挿入する文字数
s - 挿入する文字文字列へのポインタ
str - 挿入する文字列
first, last - 挿入する文字を定義する範囲
s_index - `str` における挿入する最初の文字の位置
ilist - 文字を挿入する `std::initializer_list`
t - 文字を挿入するオブジェクト(`std::basic_string_view` に変換可能)
t_index - `t` における挿入する最初の文字の位置
型要件
-
InputItLegacyInputIterator の要件を満たす必要があります。

[編集] 戻り値

1-5) `*this`
6-9) 挿入された最初の文字のコピーを参照するイテレータ、または文字が挿入されなかった場合(`count == 0`、`first == last`、または `ilist.size() == 0` の場合)は `pos`。
10,11) `*this`

[編集] 例外

1-4,10) `index > size()` の場合、`std::out_of_range` をスローします。
5) `index > size()` または `s_index > str.size()` の場合、`std::out_of_range` をスローします。
11) `index > size()` または `t_index > sv.size()` の場合、`std::out_of_range` をスローします。

すべての場合において、`size() + ins_count > max_size()` の場合(`ins_count` は挿入される文字数)、`std::length_error` をスローします。

すべての場合において、`std::allocator_traits::allocate` が例外をスローした場合、それは再スローされます。

(C++20以降)

何らかの理由で例外がスローされた場合、この関数は効果がありません(強力な例外安全保証)。

[編集]

#include <cassert>
#include <iterator>
#include <string>
 
using namespace std::string_literals;
 
int main()
{
    std::string s = "xmplr";
 
    // insert(size_type index, size_type count, char ch)
    s.insert(0, 1, 'E');
    assert("Exmplr" == s);
 
    // insert(size_type index, const char* s)
    s.insert(2, "e");
    assert("Exemplr" == s);
 
    // insert(size_type index, string const& str)
    s.insert(6, "a"s);
    assert("Exemplar" == s);
 
    // insert(size_type index, string const& str,
    //        size_type s_index, size_type count)
    s.insert(8, " is an example string."s, 0, 14);
    assert("Exemplar is an example" == s);
 
    // insert(const_iterator pos, char ch)
    s.insert(s.cbegin() + s.find_first_of('n') + 1, ':');
    assert("Exemplar is an: example" == s);
 
    // insert(const_iterator pos, size_type count, char ch)
    s.insert(s.cbegin() + s.find_first_of(':') + 1, 2, '=');
    assert("Exemplar is an:== example" == s);
 
    // insert(const_iterator pos, InputIt first, InputIt last)
    {
        std::string seq = " string";
        s.insert(s.begin() + s.find_last_of('e') + 1,
            std::begin(seq), std::end(seq));
        assert("Exemplar is an:== example string" == s);
    }
 
    // insert(const_iterator pos, std::initializer_list<char>)
    s.insert(s.cbegin() + s.find_first_of('g') + 1, {'.'});
    assert("Exemplar is an:== example string." == s);
}

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 7 C++98 オーバーロード (8) は、存在しないオーバーロードを参照していました。 正しくオーバーロード (4) を参照します。
LWG 847 C++98 例外安全性保証がなかった 強力な例外安全性保証を追加
LWG 2946 C++17 オーバーロード (10) は、一部のケースで曖昧さを引き起こしました。 テンプレートにすることで回避されました。

[編集] 関連項目

文字の範囲を挿入します
(public member function) [編集]
末尾に文字を追加する
(public member function) [編集]
末尾に文字を追加する
(public member function) [編集]
"https://ja.cppreference.dev/mwiki/index.php?title=cpp/string/basic_string/insert&oldid=171131" から取得
English 日本語 中文(简体) 中文(繁體)