std::basic_string<CharT,Traits,Allocator>::insert
cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| (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 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 basic_string& insert( size_type index, const basic_string& str, size_type s_index, 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 StringViewLike > basic_string& insert( size_type index, const StringViewLike& t ); |
( C++17) ( C++20) |
|
template< class StringViewLike > constexpr basic_string& insert( size_type index, const StringViewLike& t ); |
( C++20) | |
| (11) | ||
template< class StringViewLike > basic_string& insert( size_type index, const StringViewLike& t, size_type t_index, size_type count = npos ); |
( C++17) ( C++20) |
|
template< class StringViewLike > constexpr basic_string& insert( size_type index, const StringViewLike& t, size_type t_index, size_type count = npos ); |
( C++20) | |
.
1)
count ch index.2) ,
s, index. Traits::length(s). 3)
[s, s + count) index. .4)
str index.5) ,
str.substr(s_index, count) index.6)
ch , pos.7)
count ch ( ), pos.8)
[first, last) ( ), pos, insert(pos - begin(), basic_string(first, last, get_allocator())).
|
, |
( C++11) |
9)
ilist ( ), pos.10)
t sv, std::basic_string_view<CharT, Traits> sv = t;, sv ( ), index, insert(index, sv.data(), sv.size()). ,
std::is_convertible_v<const StringViewLike&,std::basic_string_view<CharT, Traits>> true std::is_convertible_v<const StringViewLike&, const CharT*> false.
11)
t sv, std::basic_string_view<CharT, Traits> sv = t;, ( ), index, [t_index, t_index + count) sv.
-
svcount == npos,[t_index,sv.size()). -
t_index > sv.size()index > size(), std::out_of_range.
,
std::is_convertible_v<const StringViewLike&,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.
| ||
1-5)
*this6-9) ,
pos, (count == 0 first == last ilist.size() == 0)10,11)
*this std::length_error, size() + ins_count > max_size(), ins_count , .
|
, |
( 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++:
| LWG 7 | C++98 | (8) | (4) |
| LWG 847 | C++98 | | |
| LWG 2946 | C++17 | (10) | , |
(C++23) |
(public -) |
| (public -) | |
| (public -) |