Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ string::push_back function is used to add a new character at the end of the string. Addition of new character always occurs after its current last character and every addition results into increasing the string size by one.

Syntax

void push_back (char c);
void push_back (char c);

Parameters

c Specify the new character which need to be added in the string.

Return Value

None.

Time Complexity

Constant i.e, Θ(1).

Example:

In the below example, the string::push_back function is used to add new characters in the string called MyString.

#include <iostream>
#include <string>

using namespace std;
 
int main (){
  string MyString;

  //add new characters in the string
  MyString.push_back('H');
  MyString.push_back('E');
  MyString.push_back('L');
  MyString.push_back('L');
  MyString.push_back('O');

  cout<<MyString;

  return 0;
}

The output of the above code will be:

HELLO

❮ C++ <string> Library