public member function
<string>

std::basic_string::operator+=

string (1)
basic_string& operator+= (const basic_string& str);
c-string (2)
basic_string& operator+= (const charT* s);
character (3)
basic_string& operator+= (charT c);
string (1)
basic_string& operator+= (const basic_string& str);
c-string (2)
basic_string& operator+= (const charT* s);
character (3)
basic_string& operator+= (charT c);
initializer list (4)
basic_string& operator+= (initializer_list<charT> il);
Append to string
Extends the basic_string by appending additional characters at the end of its current value

(See member function append for additional appending options).

引數

str
A basic_string object of the same type (with the same class template argumentscharT, 特性 (traits)Alloc), whose value is copied at the end.
s
指向以null結尾的字元序列的指標。
The sequence is copied at the end of the string.
The length is determined by callingtraits_type::length(s).
c
A character, which is appended to the current value of the string.
il
il
這些物件是從初始化列表宣告符自動構造的。
The characters are appended to the string, in the same order.

charTis basic_string's character type (i.e., its first template parameter).

返回值

*this

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// string::operator+=
#include <iostream>
#include <string>

int main ()
{
  std::string name ("John");
  std::string family ("Smith");
  name += " K. ";         // c-string
  name += family;         // string
  name += '\n';           // character

  std::cout << name;
  return 0;
}

輸出
John K. Smith


複雜度

Unspecified, but generally up to linear in the new string length.

迭代器有效性

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

資料競爭

物件被修改。

異常安全

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

If the resulting string length would exceed the max_size, a length_error exception is thrown.
如果該型別使用預設分配器,如果函式需要分配儲存空間但失敗,則會丟擲bad_alloc異常。

另見