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 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, size_type index_str, size_type count = npos); |
(C++17) (C++20) |
|
template < class T > constexpr basic_string& insert( size_type index, const T& t, size_type index_str, size_type count = npos); |
(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 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)
*this6-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);
}
| () | |
| () |