std::basic_string<CharT,Traits,Allocator>::append
来自 cppreference.cn
< cpp | string | basic_string
basic_string& append( size_type count, CharT ch ); |
(1) | (C++20 起为 constexpr) |
basic_string& append( const CharT* s, size_type count ); |
(2) | (C++20 起为 constexpr) |
basic_string& append( const CharT* s ); |
(3) | (C++20 起为 constexpr) |
template< class SV > basic_string& append( const SV& t ); |
(4) | (C++17 起) (C++20 起为 constexpr) |
template< class SV > basic_string& append( const SV& t, size_type pos, |
(5) | (C++17 起) (C++20 起为 constexpr) |
basic_string& append( const basic_string& str ); |
(6) | (C++20 起为 constexpr) |
(7) | ||
basic_string& append( const basic_string& str, size_type pos, size_type count ); |
(直到 C++14) | |
basic_string& append( const basic_string& str, size_type pos, size_type count = npos ); |
(C++14 起) (C++20 起为 constexpr) |
|
template< class InputIt > basic_string& append( InputIt first, InputIt last ); |
(8) | (C++20 起为 constexpr) |
basic_string& append( std::initializer_list<CharT> ilist ); |
(9) | (C++11 起) (C++20 起为 constexpr) |
将附加字符追加到字符串。
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));。 |
(C++20 起) |
8) 等价于 return append(basic_string(first, last, get_allocator()));。
如果 |
(C++11 前) |
此重载仅当 |
(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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 847 | C++98 | 没有异常安全保证 | 添加了强异常安全保证 |
LWG 2250 | C++98 | 重载 (7) 的行为是 如果 pos > str.size() 为 true 则未定义 |
在这种情况下总是抛出异常 |
LWG 2788 | C++98 | 重载 (8) 使用默认构造的 分配器构造临时字符串 |
获取分配器 从 get_allocator() |
LWG 2946 | C++17 | 重载 (4) 在某些情况下会导致歧义 | 通过使其成为模板来避免 |
[编辑] 参阅
(C++23) |
将字符范围追加到末尾 (公共成员函数) |
将字符追加到末尾 (公共成员函数) | |
连接两个字符串 (函数) | |
连接两个字符串的特定数量的字符 (函数) | |
将一个宽字符串的副本追加到另一个宽字符串 (函数) | |
将一定数量的宽字符从一个宽字符串附加到另一个宽字符串 (函数) |