std::basic_string<CharT,Traits,Allocator>::append

来自cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::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,

                      size_type count = npos );
(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) 后附 countch 的副本。
2) 后附范围 [ss + count) 中的字符。
如果 [ss + count) 不是有效范围,那么行为未定义。
3) 等价于 return append(s, Traits::length(s));
4,5) 后附从 t 构造的字符串视图 sv 中的字符。
  • 如果只提供了 t,那么就会后附 sv 中的所有字符。
  • 如果也提供了 pos
    • 如果 countnpos,那么就会后附 sv 中从 pos 处开始的所有字符。
    • 否则会后附 sv 中从 pos 处开始的 std::min(count, sv.size() - pos) 个字符。
这些重载只有在满足以下所有条件时才会参与重载决议:
4) 等价于 std::basic_string_view<CharT, Traits> sv = t;
return append(sv.data(), sv.size());
5) 等价于 std::basic_string_view<CharT, Traits> sv = t;
return append(sv.substr(pos, count));
6,7) 后附另一字符串 str 中的字符。
  • 如果只提供了 str,那么就会后附 str 中的所有字符。
  • 如果也提供了 pos
    • 如果 countnpos,那么就会后附 str 中从 pos 处开始的所有字符。
    • 否则会后附 str 中从 pos 处开始的 std::min(count, str.size() - pos) 个字符。
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()));

如果 InputIt 为整数类型,那么此重载与重载 (1) 拥有相同效果。

(C++11 前)

此重载只有在 InputIt 满足 老式输入迭代器 (LegacyInputIterator) 的要求时才会参与重载决议。

(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

5) 如果 pos > sv.size()true,那么就会抛出 std::out_of_range
7) 如果 pos > str.size()true,那么就会抛出 std::out_of_range

如果因为任何原因抛出了异常,那么此函数无效果(强异常安全保证)。

[编辑] 示例

#include <cassert>
#include <string>
 
int main()
{
    std::basic_string<char> str = "string";
    const char* cptr = "C-string";
    const char carr[] = "Two and one";
 
    std::string result;
 
    // 1) 后附 char 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 pos > str.size()true 时重载 (7) 的行为未定义 此时始终会抛出异常
LWG 2788 C++98 重载 (8) 使用默认构造的分配器来构造临时字符串 get_allocator() 获取分配器
LWG 2946 C++17 重载 (4) 在某些情况下会导致歧义 通过使之为模板来避免

[编辑] 参阅

后附范围内的字符到结尾
(公开成员函数) [编辑]
后附字符到结尾
(公开成员函数) [编辑]
连接两个字符串
(函数) [编辑]
连接两个字符串的一定量字符
(函数) [编辑]
追加宽字符串的副本到另一个
(函数) [编辑]
追加来自宽字符串的一定量宽字符到另一个
(函数) [编辑]