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异常。

另见