std::basic_string<CharT,Traits,Allocator>::insert
来自cppreference.com
< cpp | string | basic string
| (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 起) | |
插入字符到 string 中。
1) 在位置
index 插入 count 个字符 ch 的副本。2) 在位置
index 插入 s 所指向的空终止字符串。字符串的长度由首个空字符,用 Traits::length(s) 确定。3) 在位置
index 插入范围 [s, s+count) 中的字符。范围能含有空字符。4) 在位置
index 插入 string str 。5) 在位置
index 插入由 str.substr(index_str, count) 获得的 string 。6) 在
pos 所指向的字符前插入字符 ch 。7) 在
pos 所指向的元素(若存在)前插入 count 个字符 ch 的副本。8) 在
pos 所指向的元素(若存在)前插入来自范围 [first, last) 的元素。若 InputIt 不满足遗留输入迭代器 (LegacyInputIterator) 则此重载不参与重载决议。 (C++11 起)9) 在
pos 所指向的元素(若存在)前插入来自 initializer_list ilist 的字符。10) 如同用 std::basic_string_view<CharT, Traits> sv = t; 隐式转换
t 为 string_view 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 为 string_view sv ,然后在 pos 所指向的元素(若存在)前插入来自 sv 的子视图 [index_str, index_str+count) 的元素。若请求的子视图越过 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 | - | 插入内容到的位置 |
| pos | - | 将插入字符到其前的迭代器 |
| ch | - | 要插入的字符 |
| count | - | 要插入的字符数 |
| s | - | 指向要插入的字符串的指针 |
| str | - | 要插入的 string |
| first, last | - | 定义要插入字符的范围 |
| index_str | - | string str 中要插入的首字符位置
|
| ilist | - | 要插入的字符来源的 std::initializer_list |
| t | - | 要插入的字符来源对象(可转换为 std::basic_string_view ) |
| 类型要求 | ||
-InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
| ||
返回值
1-5,10-11) *this
6-9) 指代首个被插入字符的迭代器,或若未插入字符则为
pos ( count == 0 或 ilist.size() == 0 或 first == last )异常
所有情况下,若 size() + ins_count > max_size() 则抛出 std::length_error ,其中 ins_count 是将要插入的字符数。并且可能抛出 Allocator::allocate 所抛的任何异常。
|
任何情况下,若因任何原因抛异常,则此函数无效果(强异常保证)。 |
(C++11 起) |
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| DR | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2946 | C++17 | string_view 重载在某些情况下导致歧义
|
通过使之为模板来避免 |
示例
运行此代码
#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); }
参阅
| 后附字符到结尾 (公开成员函数) | |
| 后附字符到结尾 (公开成员函数) |