public member function
<string>

std::basic_string::push_back

void push_back (charT c);
Append character to string
Appends character c to the end of the basic_string, increasing its length by one.

参数

c
Character added to the basic_string.
charTbasic_string 的字符类型(即,它的第一个模板参数)。

返回值



示例

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

int main ()
{
  std::string str;
  std::ifstream file ("test.txt",std::ios::in);
  if (file) {
    while (!file.eof()) str.push_back(file.get());
  }
  std::cout << str << '\n';
  return 0;
}

This example reads an entire file character by character, appending each character to a string object usingpush_back.

复杂度

Unspecified; Generally amortized constant, but up to linear in the new string length.

迭代器有效性

与此对象相关的任何迭代器、指针和引用都可能失效。

数据竞争

对象被修改。

异常安全

强保证: 如果抛出异常,则 basic_string 中没有任何更改。

如果结果 字符串长度 超过 max_size,则会抛出 length_error 异常。
如果该类型使用默认分配器,如果函数需要分配存储空间但失败,则会抛出bad_alloc异常。

另见