C++ 字串庫 - push_back



描述

它將字元 c 附加到字串的末尾,並將字串長度增加一。

宣告

以下是 std::string::push_back 的宣告。

void push_back (char c);

C++11

void push_back (char c);

C++14

void push_back (char c);

引數

c − 它是一個字元物件。

返回值

異常

如果丟擲異常,字串不會發生任何更改。

示例

以下示例演示了 std::string::push_back 的用法。

#include <iostream>
#include <fstream>
#include <string>

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