std::basic_string<CharT,Traits,Allocator>:: append
|
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) |
向字符串追加附加字符。
[
s
,
s
+
count
)
内的字符。
- std:: is_convertible_v < const SV & , std:: basic_string_view < CharT, Traits >> 为 true 。
- std:: is_convertible_v < const SV & , const CharT * > 为 false 。
return append ( sv. data ( ) , sv. size ( ) ) ; 。
return append ( sv. substr ( pos, count ) ) ; 。
|
7)
等效于
return
append
(
std::
basic_string_view
<
CharT, Traits
>
( str ) . substr ( pos, count ) ) ; 。 |
(C++20 起) |
|
若
|
(C++11 前) |
|
仅当
|
(C++11 起) |
目录 |
参数
| 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) 重复追加字符3次 // 注意:这是唯一接受“CharT”类型的重载 result.append(3, '*'); assert(result == "***"); // 2) 追加定长C字符串 result.append(cptr, 5); assert(result == "***C-str"); // 3) 追加以空字符结尾的C字符串 // 注意:由于“append”返回*this,我们可以链式调用 result.append(1, ' ').append(cptr); assert(result == "***C-str C-string"); // 6) 追加整个字符串 result.append(1, ' ').append(str); assert(result == "***C-str C-string std::string"); // 7) 追加字符串的一部分 result.append(str, 3, 2); assert(result == "***C-str C-string std::string::"); // 8) 追加范围 result.append(&carr[2], &carr[3]); assert(result == "***C-str C-string std::string::n"); // 9) 追加初始化列表 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)
|
向末尾追加一个字符范围
(公开成员函数) |
|
向末尾追加字符
(公开成员函数) |
|
|
连接两个字符串
(函数) |
|
|
连接两个字符串的指定数量字符
(函数) |
|
|
将一个宽字符串的副本追加到另一个宽字符串
(函数) |
|
|
将一个宽字符串的指定数量宽字符追加到另一个宽字符串
(函数) |