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

另見