std::basic_string<CharT,Traits,Allocator>::append
来自 cppreference.cn
< cpp | string | basic string
basic_string& append( size_type count, CharT ch ); |
(1) | (constexpr since C++20) |
basic_string& append( const CharT* s, size_type count ); |
(2) | (constexpr since C++20) |
basic_string& append( const CharT* s ); |
(3) | (constexpr since C++20) |
template< class SV > basic_string& append( const SV& t ); |
(4) | (since C++17) (constexpr since C++20) |
template< class SV > basic_string& append( const SV& t, size_type pos, |
(5) | (since C++17) (constexpr since C++20) |
basic_string& append( const basic_string& str ); |
(6) | (constexpr since C++20) |
(7) | ||
basic_string& append( const basic_string& str, size_type pos, size_type count ); |
(until C++14) | |
basic_string& append( const basic_string& str, size_type pos, size_type count = npos ); |
(since C++14) (constexpr since C++20) |
|
template< class InputIt > basic_string& append( InputIt first, InputIt last ); |
(8) | (constexpr since C++20) |
basic_string& append( std::initializer_list<CharT> ilist ); |
(9) | (since C++11) (constexpr since C++20) |
向字符串追加额外的字符。
1) 追加 count 个字符 ch 的副本。
2) 追加范围
[
s,
s + count)
内的字符。 如果
[
s,
s + count)
不是有效范围,则行为未定义。3) 等价于 return append(s, Traits::length(s));。
4,5) 追加从 t 构造的字符串视图 sv 中的字符。
仅当满足以下所有条件时,这些重载才参与重载决议
- std::is_convertible_v<const SV&, std::basic_string_view<CharT, Traits>> 为 true。
- std::is_convertible_v<const SV&, const CharT*> 为 false。
4) 等价于 std::basic_string_view<CharT, Traits> sv = t;
return append(sv.data(), sv.size());。
return append(sv.data(), sv.size());。
5) 等价于 std::basic_string_view<CharT, Traits> sv = t;
return append(sv.substr(pos, count));。
return append(sv.substr(pos, count));。
6,7) 追加另一个字符串 str 中的字符。
6) 等价于 return append(str.data(), str.size());。
7) 等价于 return append(std::basic_string_view<CharT, Traits>
(str).substr(pos, count));。 |
(since C++20) |
8) 等价于 return append(basic_string(first, last, get_allocator()));。
如果 |
(until C++11) |
仅当 |
(since C++11) |
9) 等价于 return append(ilist.begin(), ilist.size());。
目录 |
[编辑] 参数
count | - | 要追加的字符数 |
ch | - | 要追加的字符值 |
s | - | 指向要追加的字符字符串的指针 |
t | - | 可转换为 std::basic_string_view 的对象,包含要追加的字符 |
pos | - | 要追加的第一个字符的索引 |
str | - | 要追加的字符串 |
first, last | - | 要追加的字符范围 |
ilist | - | 包含要追加的字符的初始化器列表 |
[编辑] 返回值
*this
[编辑] 复杂度
没有标准的复杂度保证,典型的实现行为类似于 std::vector::insert()。
[编辑] 异常
如果操作会导致 size()
超过 max_size()
,则抛出 std::length_error。
如果由于任何原因抛出异常,则此函数不起作用(强异常安全保证)。
[编辑] 示例
运行此代码
#include <cassert> #include <string> int main() { std::string str = "std::string"; const char* cptr = "C-string"; const char carr[] = "range"; std::string result; // 1) Append a char 3 times. // Note: This is the only overload accepting “CharT”s. result.append(3, '*'); assert(result == "***"); // 2) Append a fixed-length C-string result.append(cptr, 5); assert(result == "***C-str"); // 3) Append a null-terminated C-string // Note: Because “append” returns *this, we can chain calls together. result.append(1, ' ').append(cptr); assert(result == "***C-str C-string"); // 6) Append a whole string result.append(1, ' ').append(str); assert(result == "***C-str C-string std::string"); // 7) Append part of a string result.append(str, 3, 2); assert(result == "***C-str C-string std::string::"); // 8) Append range result.append(&carr[2], &carr[3]); assert(result == "***C-str C-string std::string::n"); // 9) Append initializer list result.append({'p', 'o', 's'}); assert(result == "***C-str C-string std::string::npos"); }
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 847 | C++98 | 没有异常安全保证 | 添加了强异常安全保证 |
LWG 2250 | C++98 | 重载 (7) 的行为是 如果 pos > str.size() 为 true,则未定义 |
在这种情况下始终抛出异常 |
LWG 2788 | C++98 | 重载 (8) 使用默认构造的 分配器来构造临时字符串 |
从 get_allocator() 获取分配器 从 get_allocator() |
LWG 2946 | C++17 | 重载 (4) 在某些情况下引起歧义 | 通过使其成为模板来避免 |
[编辑] 参见
(C++23) |
将字符范围追加到末尾 (public member function) |
将字符追加到末尾 (public member function) | |
连接两个字符串 (function) | |
连接两个字符串的指定数量的字符 (function) | |
将一个宽字符串的副本追加到另一个宽字符串 (function) | |
将一个宽字符串的指定数量的宽字符追加到另一个宽字符串 (function) |