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 str.

#include <iostream>
#include <string>

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

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

  cout<<str;

  return 0;
}

The output of the above code will be:

HELLO

❮ C++ <string> Library