「cpp/string/basic string/insert」の版間の差分
提供: cppreference.com
< cpp | string | basic string
(P0980R1) |
|||
2行: | 2行: | ||
{{cpp/string/basic_string/navbar}} | {{cpp/string/basic_string/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl | num=1 | | + | {{dcl | num=1 |
+ | | | ||
basic_string& insert( size_type index, size_type count, CharT ch ); | basic_string& insert( size_type index, size_type count, CharT ch ); | ||
+ | |||
+ | |||
}} | }} | ||
− | {{dcl | num=2 | | + | {{dcl | num=2 |
+ | | | ||
basic_string& insert( size_type index, const CharT* s ); | basic_string& insert( size_type index, const CharT* s ); | ||
+ | |||
+ | |||
}} | }} | ||
− | {{dcl | num=3 | | + | {{dcl | num=3 |
+ | | | ||
basic_string& insert( size_type index, const CharT* s, size_type count ); | basic_string& insert( size_type index, const CharT* s, size_type count ); | ||
+ | |||
+ | |||
+ | |||
}} | }} | ||
− | {{dcl | num=4 | | + | {{dcl | num=4 |
+ | | | ||
basic_string& insert( size_type index, const basic_string& str ); | basic_string& insert( size_type index, const basic_string& str ); | ||
+ | |||
+ | |||
}} | }} | ||
− | {{dcl rev | + | {{dcl rev | num=5 |
− | + | | = | |
basic_string& insert( size_type index, const basic_string& str, | basic_string& insert( size_type index, const basic_string& str, | ||
size_type index_str, size_type count ); | size_type index_str, size_type count ); | ||
− | + | | =c++14 | | |
− | + | ||
basic_string& insert( size_type index, const basic_string& str, | basic_string& insert( size_type index, const basic_string& str, | ||
− | size_type index_str, size_type count | + | size_type index_str, size_type count = |
+ | |||
+ | |||
+ | npos); | ||
}} | }} | ||
− | {{dcl rev | + | {{dcl rev | num=6 |
− | + | | = | |
− | + | ||
iterator insert( iterator pos, CharT ch ); | iterator insert( iterator pos, CharT ch ); | ||
− | + | | =c++11 | | |
− | + | ||
iterator insert( const_iterator pos, CharT ch ); | iterator insert( const_iterator pos, CharT ch ); | ||
+ | |||
+ | |||
}} | }} | ||
− | {{dcl rev | + | {{dcl rev | num=7 |
− | + | | = | |
− | + | ||
void insert( iterator pos, size_type count, CharT ch ); | void insert( iterator pos, size_type count, CharT ch ); | ||
− | + | | =c++11 | | |
− | + | ||
iterator insert( const_iterator pos, size_type count, CharT ch ); | iterator insert( const_iterator pos, size_type count, CharT ch ); | ||
+ | |||
+ | |||
}} | }} | ||
− | {{dcl rev | + | {{dcl rev | num=8 |
− | + | | = | |
− | + | ||
template< class InputIt > | template< class InputIt > | ||
void insert( iterator pos, InputIt first, InputIt last ); | void insert( iterator pos, InputIt first, InputIt last ); | ||
− | + | | =c++11 | | |
− | + | ||
template< class InputIt > | template< class InputIt > | ||
iterator insert( const_iterator pos, InputIt first, InputIt last ); | iterator insert( const_iterator pos, InputIt first, InputIt last ); | ||
+ | |||
+ | |||
+ | |||
}} | }} | ||
− | {{dcl rev | + | {{dcl rev | num=9 |
− | + | | =c++11 | | |
iterator insert( const_iterator pos, std::initializer_list<CharT> ilist ); | iterator insert( const_iterator pos, std::initializer_list<CharT> ilist ); | ||
+ | |||
+ | |||
+ | |||
}} | }} | ||
− | {{dcl rev | + | {{dcl rev | num=10 |
− | + | | =c++17 | | |
template < class T > | template < class T > | ||
basic_string& insert( size_type pos, const T& t ); | basic_string& insert( size_type pos, const T& t ); | ||
+ | |||
+ | |||
+ | |||
}} | }} | ||
− | {{dcl | num=11 | | + | {{dcl | num=11 |
+ | | =c++17 | = | ||
template < class T > | template < class T > | ||
basic_string& insert( size_type index, const T& t, | basic_string& insert( size_type index, const T& t, | ||
size_type index_str, size_type count = npos); | size_type index_str, size_type count = npos); | ||
+ | |||
+ | |||
+ | |||
+ | |||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
117行: | 144行: | ||
すべてのケースにおいて、 {{c|size() + ins_count > max_size()}} の場合は {{lc|std::length_error}} を投げます。 ただし {{tt|ins_count}} は挿入される文字数です。 また、 {{tt|Allocator::allocate}} によって投げられるあらゆる例外を投げる可能性があります。 | すべてのケースにおいて、 {{c|size() + ins_count > max_size()}} の場合は {{lc|std::length_error}} を投げます。 ただし {{tt|ins_count}} は挿入される文字数です。 また、 {{tt|Allocator::allocate}} によって投げられるあらゆる例外を投げる可能性があります。 | ||
− | {{ | + | {{|since=c++11| |
− | + | いずれのケースでも、何らかの理由で例外が投げられた場合、この関数は効果を持ちません (強い例外保証)。 | |
− | いずれのケースでも、何らかの理由で例外が投げられた場合、この関数は効果を持ちません (強い例外保証)。 | + | }} |
− | + | ||
===欠陥報告=== | ===欠陥報告=== |
2020年3月1日 (日) 09:40時点における最新版
(1) | ||
basic_string& insert( size_type index, size_type count, CharT ch ); |
(C++20未満) | |
constexpr basic_string& insert( size_type index, size_type count, CharT ch ); |
(C++20以上) | |
(2) | ||
basic_string& insert( size_type index, const CharT* s ); |
(C++20未満) | |
constexpr basic_string& insert( size_type index, const CharT* s ); |
(C++20以上) | |
(3) | ||
basic_string& insert( size_type index, const CharT* s, size_type count ); |
(C++20未満) | |
constexpr basic_string& insert( size_type index, const CharT* s, size_type count ); |
(C++20以上) | |
(4) | ||
basic_string& insert( size_type index, const basic_string& str ); |
(C++20未満) | |
constexpr basic_string& insert( size_type index, const basic_string& str ); |
(C++20以上) | |
(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以上) (C++20未満) |
|
constexpr basic_string& insert( size_type index, const basic_string& str, size_type index_str, size_type count = npos); |
(C++20以上) | |
(6) | ||
iterator insert( iterator pos, CharT ch ); |
(C++11未満) | |
iterator insert( const_iterator pos, CharT ch ); |
(C++11以上) (C++20未満) |
|
constexpr iterator insert( const_iterator pos, CharT ch ); |
(C++20以上) | |
(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 iterator insert( const_iterator pos, size_type count, CharT ch ); |
(C++20以上) | |
(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未満) |
|
template< class InputIt > constexpr iterator insert( const_iterator pos, InputIt first, InputIt last ); |
(C++20以上) | |
(9) | ||
iterator insert( const_iterator pos, std::initializer_list<CharT> ilist ); |
(C++11以上) (C++20未満) |
|
constexpr iterator insert( const_iterator pos, std::initializer_list<CharT> ilist ); |
(C++20以上) | |
(10) | ||
template < class T > basic_string& insert( size_type pos, const T& t ); |
(C++17以上) (C++20未満) |
|
template < class T > constexpr basic_string& insert( size_type pos, const T& t ); |
(C++20以上) | |
(11) | ||
template < class T > basic_string& insert( size_type index, const T& t, |
(C++17以上) (C++20未満) |
|
template < class T > constexpr basic_string& insert( size_type index, const T& t, |
(C++20以上) | |
文字列に文字を挿入します。
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); }
[編集] 関連項目
文字を末尾に追加します (パブリックメンバ関数) | |
文字を末尾に追加します (パブリックメンバ関数) |