名稱空間
變體
操作

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) 追加 count 個字元 ch 的副本。
2) 追加範圍 [ss + count) 中的字元。
如果 [ss + count) 不是有效範圍,則行為未定義。
3) 等價於 return append(s, Traits::length(s));
4,5) 追加從 t 構造的字串檢視 sv 中的字元。
  • 如果只提供了 t,則追加 sv 中的所有字元。
  • 如果也提供了 pos
    • 如果 countnpos,則追加從 pos 開始的 sv 中的所有字元。
    • 否則,追加從 pos 開始的 svstd::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,則追加其中的所有字元。
  • 如果也提供了 pos
    • 如果 countnpos,則追加從 pos 開始的 str 中的所有字元。
    • 否則,追加從 pos 開始的 strstd::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::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) 在某些情況下會導致歧義 透過使其成為模板來避免

[編輯] 參閱

將字元範圍追加到末尾
(公共成員函式) [編輯]
將字元追加到末尾
(公共成員函式) [編輯]
連線兩個字串
(函式) [編輯]
連線兩個字串的特定數量的字元
(函式) [編輯]
將一個寬字串的副本追加到另一個寬字串
(函式) [編輯]
將一定數量的寬字元從一個寬字串附加到另一個寬字串
(函式) [編輯]