std::basic_string<CharT,Traits,Allocator>:: insert
|
basic_string
&
insert
(
size_type index, size_type count, CharT ch
)
;
|
(1) | (constexpr since C++20) |
|
basic_string
&
insert
(
size_type index,
const
CharT
*
s
)
;
|
(2) | (constexpr since C++20) |
|
basic_string
&
insert
(
size_type index,
const
CharT
*
s, size_type count
)
;
|
(3) | (constexpr since C++20) |
|
basic_string
&
insert
(
size_type index,
const
basic_string
&
str
)
;
|
(4) | (constexpr since C++20) |
| (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以降)
(constexprはC++20以降) |
|
| (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,
|
(11) |
(C++17以降)
(C++20以降constexpr) |
文字列に文字を挿入します。
[
s
,
s
+
count
)
の文字を挿入します。この範囲にはナル文字を含めることができます。
[
first
,
last
)
からの文字を挿入します。挿入は
insert
(
pos
-
begin
(
)
, basic_string
(
first, last, get_allocator
(
)
)
)
によって行われるかのようになります。
|
このオーバーロードは、
|
(C++11以降) |
std:: basic_string_view < CharT, Traits >> が true であり、かつ std:: is_convertible_v < const StringViewLike & , const CharT * > が false である場合。
[
t_index
,
t_index
+
count
)
から文字を挿入します。
-
要求された部分ビューが
sv
の終端を超える場合、または
count
==
npos
の場合、結果の部分ビューは
[t_index,sv. size ( ))となります。 - t_index > sv. size ( ) の場合、または index > size ( ) の場合、 std::out_of_range がスローされます。
std:: basic_string_view < CharT, Traits >> が true であり、かつ std:: is_convertible_v < const StringViewLike & , const CharT * > が false である場合です。
pos が * this に対する有効なイテレータでない場合、動作は未定義です。
目次 |
パラメータ
| index | - | コンテンツが挿入される位置 |
| pos | - | 文字が挿入される前のイテレータ |
| ch | - | 挿入する文字 |
| count | - | 挿入する文字数 |
| s | - | 挿入する文字列へのポインタ |
| str | - | 挿入する文字列 |
| first, last | - | 挿入する文字を定義する範囲 |
| s_index | - | str 内で挿入する最初の文字の位置 |
| ilist | - | std::initializer_list から文字を挿入 |
| t | - | 文字を挿入するオブジェクト( std::basic_string_view に変換可能) |
| t_index | - | t 内で挿入する最初の文字の位置 |
| 型要件 | ||
-
InputIt
は
LegacyInputIterator
の要件を満たさなければならない。
|
||
戻り値
例外
すべての場合において、 std::length_error をスローする( size ( ) + ins_count > max_size ( ) の場合)。ここで ins_count は挿入される文字数である。
|
すべての場合において、 std:: allocator_traits < Allocator > :: allocate が例外をスローした場合、その例外は再スローされます。 |
(C++20以降) |
何らかの理由で例外がスローされた場合、この関数は何も効果を持ちません( strong exception safety guarantee )。
例
#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) が一部の場合に曖昧性を引き起こした | テンプレート化することで回避 |
関連項目
|
(C++23)
|
文字の範囲を挿入する
(公開メンバ関数) |
|
末尾に文字を追加する
(公開メンバ関数) |
|
|
末尾に1文字を追加する
(公開メンバ関数) |