在线文档教程
C++
字符串 | Strings

std::basic_string::insert

性病:基本[医]字符串:插入

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 (until C++14)
basic_string& insert( size_type index, const basic_string& str, size_type index_str, size_type count = npos(since C++14)
(6)
iterator insert( iterator pos, CharT ch (until C++11)
iterator insert( const_iterator pos, CharT ch (since C++11)
(7)
void insert( iterator pos, size_type count, CharT ch (until C++11)
iterator insert( const_iterator pos, size_type count, CharT ch (since C++11)
(8)
template< class InputIt > void insert( iterator pos, InputIt first, InputIt last (until C++11)
template< class InputIt > iterator insert( const_iterator pos, InputIt first, InputIt last (since C++11)
iterator insert( const_iterator pos, std::initializer_list<CharT> ilist (9)(since C++11)
basic_string& insert( size_type pos, basic_string_view<CharT, Traits> sv (10)(since C++17)
template < class T > basic_string& insert( size_type index, const T& t, size_type index_str, size_type count = npos(11)(since C++17)

将字符插入字符串。

1%29次插入count字符副本ch在这个位置index

2%29插入以空结尾的字符串。s在这个位置index字符串的长度由第一个空字符%28有效调用决定Traits::length(s)...

3%29插入第一个count指向的字符串中的字符。s在这个位置index...s可以包含空字符。

4%29插入字符串str在这个位置index

5%29插入字符串,由str.substr(index_str, count)在这个位置index

6%29插入字符ch之前的字符pos

7%29次插入count字符副本ch在由pos

8%29插入范围内的字符。[first, last)在由pos此重载不参与重载解决方案。InputIt不满意InputIterator.%28自C++11%29

9%29从初始化程序列表插入元素ilist在由pos

10%29从字符串视图插入元素。sv在由pos,好像insert(pos, sv.data(), sv.size())

11%29名皈依者t到字符串视图sv好像std::basic_string_view<CharT, Traits> sv = t;,然后插入pos,子视图中的字符。[index_str, index_str+count)成sv如果所请求的子视图持续到sv,或者如果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-position at which the content will be inserted
pos-iterator before which the characters will be inserted
ch-character to insert
count-number of characters to insert
s-pointer to the character string to insert
str-string to insert
first, last-range defining characters to insert
index_str-position of the first character in the string str to insert
ilist-std::initializer_list to insert the characters from
sv-std::basic_string_view to insert the characters from
t-object (convertible to std::basic_string_view) to insert the characters from

类型要求

-输入必须符合输入器的要求。

返回值

1-5,10-11%29*this

6-9%29一个迭代器,它指的是第一个插入字符的副本或pos如果没有插入字符%28count==0first==lastilist.size()==0%29

例外

1%29std::out_of_range如果index > size()和std::length_error如果size() + count > max_size()...

2%29std::out_of_range如果index > size()和std::length_error如果size() + Traits::length(s) > max_size()...

3%29std::out_of_range如果index > size()和std::length_error如果size() + count > max_size()...

4%29std::out_of_range如果index > size()和std::length_error如果size() + str.size() > max_size()...

5%29在下列条件下抛出异常:

A%29std::out_of_range如果index > size()...

B%29std::out_of_range如果index_str > str.size()...

C%29std::length_error如果size() + ins_count > max_size()何地ins_count要插入的字符数。

6-9%29%280%29

10%29std::out_of_range如果index > size()和std::length_error如果size() + sv.size() > max_size()...

11%29在下列条件下抛出异常:

A%29std::out_of_range如果index > size()...

B%29std::out_of_range如果index_str > sv.size()...

C%29std::length_error如果size() + ins_count > max_size()何地ins_count要插入的字符数。

In any case, if an exception is thrown for any reason, this function has no effect (strong exception guarantee).(since C++11)

二次

#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 }

二次

另见

appendappends characters to the end (public member function)
push_backappends a character to the end (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/string/basic[医]字符串/插入