C++ Standard Library C++ STL Library

C++ <string> - push_back() Function



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 example below, 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

5