「cpp/string/basic string/insert」の版間の差分
提供: cppreference.com
< cpp | string | basic string
69行: | 69行: | ||
@1@ 位置 {{tt|index}} に文字 {{tt|ch}} のコピーを {{tt|count}} 個挿入します。 | @1@ 位置 {{tt|index}} に文字 {{tt|ch}} のコピーを {{tt|count}} 個挿入します。 | ||
− | @2@ 位置 {{tt|index}} に {{tt|s}} の指すヌル終端文字列を挿入します。 | + | @2@ 位置 {{tt|index}} に {{tt|s}} の指すヌル終端文字列を挿入します。 {{c|Traits::length(s)}} |
− | @3@ | + | @3@ {{tt|scount}} {{tt|}} |
@4@ 位置 {{tt|index}} に文字列 {{tt|str}} を挿入します。 | @4@ 位置 {{tt|index}} に文字列 {{tt|str}} を挿入します。 | ||
79行: | 79行: | ||
@6@ {{tt|pos}} の指す文字の前に文字 {{tt|ch}} を挿入します。 | @6@ {{tt|pos}} の指す文字の前に文字 {{tt|ch}} を挿入します。 | ||
− | @7@ {{tt|pos}} | + | @7@ {{tt|pos}} {{tt|ch}} のコピーを {{tt|count}} 個挿入します。 |
− | @8@ {{tt|pos}} | + | @8@ {{tt|pos}} {{tt|[first, last)}} から文字を挿入します。 {{rev inl|since=c++11|このオーバーロードは、 {{tt|InputIt}} が {{named req|InputIterator}} を満たさない場合、オーバーロード解決に参加しません。}} |
− | @9@ {{tt|pos}} | + | @9@ {{tt|pos}} {{tt|ilist}} から要素を挿入します。 |
− | @10@ {{cpp/string/sv hack|{{c|insert(pos, sv.data(), sv.size())}} によって行われたかのように、 {{tt|pos}} | + | @10@ {{cpp/string/sv hack|{{c|insert(pos, sv.data(), sv.size())}} によって行われたかのように、 {{tt|pos}} {{tt|sv}} から要素を挿入します}} |
− | @11@ {{cpp/string/sv hack|{{tt|pos}} | + | @11@ {{cpp/string/sv hack|{{tt|pos}} {{tt|sv}} のサブビュー {{tt|[index_str, index_str+count)}} から文字を挿入します。 要求されたサブビューが終端を超える場合、または {{c|1=count == npos}} の場合、結果のサブビューは {{tt|[index_str, sv.size())}}になります。 {{c|1=index_str > sv.size()}} の場合、または {{c|index > size()}} の場合、 {{lc|std::out_of_range}} が投げられます}} |
===引数=== | ===引数=== | ||
111行: | 111行: | ||
===例外=== | ===例外=== | ||
− | @1@ {{c|index > size()}} の場合は {{lc|std::out_of_range}} | + | @1@ {{c|index > size()}} の場合は {{lc|std::out_of_range}} |
+ | {{c|size()> ()}} の場合は {{lc|std:: | ||
+ | }} | ||
− | + | {{c|size() + ins_count > max_size()}} の場合は {{lc|std::length_error}} {{tt|ins_count}} は挿入される文字数です。 {{|::}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
{{rev begin}} | {{rev begin}} |
2018年11月28日 (水) 17:36時点における版
basic_string& insert( size_type index, size_type count, CharT ch ); |
(1) | |
basic_string& insert( size_type index, const CharT* s ); |
(2) | |
basic_string& insert( size_type index, const CharT* s, size_type count ); |
(3) | |
basic_string& insert( size_type index, const basic_string& str ); |
(4) | |
(5) | ||
basic_string& insert( size_type index, const basic_string& str, size_type index_str, size_type count ); |
(C++14未満) | |
basic_string& insert( size_type index, const basic_string& str, size_type index_str, size_type count = npos); |
(C++14以上) | |
(6) | ||
iterator insert( iterator pos, CharT ch ); |
(C++11未満) | |
iterator insert( const_iterator pos, CharT ch ); |
(C++11以上) | |
(7) | ||
void insert( iterator pos, size_type count, CharT ch ); |
(C++11未満) | |
iterator insert( const_iterator pos, size_type count, CharT ch ); |
(C++11以上) | |
(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以上) | |
iterator insert( const_iterator pos, std::initializer_list<CharT> ilist ); |
(9) | (C++11以上) |
template < class T > basic_string& insert( size_type pos, const T& t ); |
(10) | (C++17以上) |
template < class T > basic_string& insert( size_type index, const T& t, |
(11) | (C++17以上) |
文字列に文字を挿入します。
1) 位置
index
に文字 ch
のコピーを count
個挿入します。2) 位置
index
に s
の指すヌル終端文字列を挿入します。 文字列の長さは Traits::length(s) を用いて最初のヌル文字によって決定されます。3) 範囲
[s, s+count)
の文字を位置 index
に挿入します。 範囲はヌル文字を含むことができます。4) 位置
index
に文字列 str
を挿入します。5) 位置
index
に str.substr(index_str, count) から取得した文字列を挿入します。6)
pos
の指す文字の前に文字 ch
を挿入します。7)
pos
の指す要素 (もしあれば) の前に文字 ch
のコピーを count
個挿入します。8)
pos
の指す要素 (もしあれば) の前に範囲 [first, last)
から文字を挿入します。 このオーバーロードは、 InputIt
が LegacyInputIterator を満たさない場合、オーバーロード解決に参加しません。 (C++11以上)9)
pos
の指す要素 (もしあれば) の前に初期化子リスト ilist
から要素を挿入します。10) std::basic_string_view<CharT, Traits> sv = t; によって行われたかのように、
t
を文字列ビュー sv
に暗黙に変換し、 insert(pos, sv.data(), sv.size()) によって行われたかのように、 pos
の指す要素 (もしあれば) の前に sv
から要素を挿入します。 このオーバーロードは、std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> が true であり、 std::is_convertible_v<const T&, const CharT*> が false である場合にのみ、オーバーロード解決に参加します。11) std::basic_string_view<CharT, Traits> sv = t; によって行われたかのように、
t
を文字列ビュー sv
に暗黙に変換し、 pos
の指す要素 (もしあれば) の前に sv
のサブビュー [index_str, index_str+count)
から文字を挿入します。 要求されたサブビューが終端を超える場合、または count == npos の場合、結果のサブビューは [index_str, sv.size())
になります。 index_str > sv.size() の場合、または index > size() の場合、 std::out_of_range が投げられます。 このオーバーロードは、std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> が true であり、 std::is_convertible_v<const T&, const CharT*> が false である場合にのみ、オーバーロード解決に参加します。目次 |
引数
index | - | 内容を挿入する位置 |
pos | - | 前に文字を挿入するイテレータ |
ch | - | 挿入する文字 |
count | - | 挿入する文字数 |
s | - | 挿入する文字列を指すポインタ |
str | - | 挿入する文字列 |
first, last | - | 挿入する文字列を定義する範囲 |
index_str | - | 挿入する文字列 str 内の最初の文字の位置
|
ilist | - | 挿入する文字の std::initializer_list |
t | - | 挿入する文字の (std::basic_string_view に変換可能な) オブジェクト |
型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
|
戻り値
1-5,10-11) *this。
6-9) 挿入された最初の文字のコピーを参照するイテレータ、または挿入された文字がなかった (
count==0
または first==last
または ilist.size()==0
) 場合は pos
。例外
すべてのケースにおいて、 size() + ins_count > max_size() の場合は std::length_error を投げます。 ただし ins_count
は挿入される文字数です。 また、 Allocator::allocate
によって投げられるあらゆる例外を投げる可能性があります。
いずれのケースでも、何らかの理由で例外が投げられた場合、この関数は効果を持ちません (強い例外保証)。 | (C++11以上) |
欠陥報告
以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。
DR | 適用先 | 発行時の動作 | 正しい動作 |
---|---|---|---|
LWG 2946 | C++17 | string_view overload causes ambiguity in some cases
|
avoided by making it a template |
例
Run this code
#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 index_str, 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); }
関連項目
文字を末尾に追加します (パブリックメンバ関数) | |
文字を末尾に追加します (パブリックメンバ関数) |