public member function
<string>

std::basic_string::append

string (1)
basic_string& append (const basic_string& str);
substring (2)
basic_string& append (const basic_string& str, size_type subpos, size_type sublen);
c-string (3)
basic_string& append (const charT* s);
buffer (4)
basic_string& append (const charT* s, size_type n);
fill (5)
basic_string& append (size_type n, charT c);
range (6)
template <class InputIterator>   basic_string& append (InputIterator first, InputIterator last);
string (1)
basic_string& append (const basic_string& str);
substring (2)
basic_string& append (const basic_string& str, size_type subpos, size_type sublen);
c-string (3)
basic_string& append (const charT* s);
buffer (4)
basic_string& append (const charT* s, size_type n);
fill (5)
basic_string& append (size_type n, charT c);
range (6)
template <class InputIterator>   basic_string& append (InputIterator first, InputIterator last);
initializer list(7)
basic_string& append (initializer_list<charT> il);
string (1)
basic_string& append (const basic_string& str);
substring (2)
basic_string& append (const basic_string& str, size_type subpos, size_type sublen = npos);
c-string (3)
basic_string& append (const charT* s);
buffer (4)
basic_string& append (const charT* s, size_type n);
fill (5)
basic_string& append (size_type n, charT c);
range (6)
template <class InputIterator>   basic_string& append (InputIterator first, InputIterator last);
initializer list(7)
basic_string& append (initializer_list<charT> il);
向字串追加內容
透過在當前值的末尾追加額外的字元來擴充套件 basic_string

(1) 字串
追加 str 的副本。
(2) 子字串
追加 str 的子字串的副本。子字串是從字元位置 subpos 開始並跨越 sublen 個字元(或直到 str 的末尾,如果 str 太短或者 sublenbasic_string::npos)的部分。
(3) C 風格字串
追加由 s 指向的以 null 結尾的字元序列(C 風格字串)形成的字串的副本。
此字元序列的長度由呼叫確定traits_type::length(s).
(4) 緩衝區
追加 s 指向的字元陣列中的前 n 個字元的副本。
(5) 填充
追加 n 個字元 c 的連續副本。
(6) 範圍
追加範圍[first,last),以相同的順序。
(7) 初始化列表
追加 il 中每個字元的副本,順序相同。

引數

str
另一個具有相同型別的basic_string物件(具有相同的類模板引數charT, 特性 (traits)Alloc), 其值被追加。
subpos
str 中作為子字串複製到物件的第一個字元的位置。
如果它大於str長度,則丟擲out_of_range
注意:str中的第一個字元用值表示0(不是1).
sublen
要複製的子字串的長度(如果字串較短,則會複製儘可能多的字元)。
basic_string::npos值表示直到str末尾的所有字元。
s
指向字元陣列的指標(例如c-string)。
n
要複製的字元數。
c
字元值,重複 *n* 次。
first, last
指向範圍內初始位置和最終位置的輸入迭代器。 使用的範圍是[first,last),其中包括firstlast之間的所有字元,包括first指向的字元,但不包括last指向的字元。
函式模板引數InputIterator應為指向可轉換為charT.
如果已知InputIterator是一種整數型別,引數被強制轉換為適當的型別,以便使用簽名 (5) 代替。
il
il
這些物件是從初始化列表宣告符自動構造的。

charTbasic_string 的字元型別(即,它的第一個模板引數)。
成員型別size_type是一種無符號整型型別。

返回值

*this

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// appending to string
#include <iostream>
#include <string>

int main ()
{
  std::string str;
  std::string str2="Writing ";
  std::string str3="print 10 and then 5 more";

  // used in the same order as described above:
  str.append(str2);                       // "Writing "
  str.append(str3,6,3);                   // "10 "
  str.append("dots are cool",5);          // "dots "
  str.append("here: ");                   // "here: "
  str.append(10u,'.');                    // ".........."
  str.append(str3.begin()+8,str3.end());  // " and then 5 more"
  str.append<int>(5,0x2E);                // "....."

  std::cout << str << '\n';
  return 0;
}

輸出
Writing 10 dots here: .......... and then 5 more.....


複雜度

未指定,但通常與新的 字串長度 成線性關係。

迭代器有效性

與此物件相關的任何迭代器、指標和引用都可能失效。

資料競爭

物件被修改。

異常安全

強保證: 如果丟擲異常,則 basic_string 中沒有任何更改。

如果已知s沒有指向足夠長的陣列,或者如果指定的範圍是[first,last)無效,則會導致未定義行為

如果 subpos 大於 str長度,則會丟擲 out_of_range 異常。
如果結果 字串長度 超過 max_size,則會丟擲 length_error 異常。
如果該型別使用預設分配器,如果函式需要分配儲存空間但失敗,則會丟擲bad_alloc異常。

另見