C++ String Library - push_back


Advertisements

Description

It appends character c to the end of the string, increasing its length by one.

Declaration

Following is the declaration for std::string::push_back.

void push_back (char c);

C++11

void push_back (char c);

C++14

void push_back (char c);

Parameters

c − It is a character object.

Return Value

none

Exceptions

if an exception is thrown, there are no changes in the string.

Example

In below example for 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
Advertisements