C++ Standard Library C++ STL Library

C++ <string> - pop_back() Function



The C++ string::pop_back function is used to delete the last character of the string. Every deletion of character results into reducing the string size by one unless the string is empty.

Syntax

void pop_back();

Parameters

No parameter is required.

Return Value

None.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the string::pop_back function is used to delete last character of the string called str.

#include <iostream>
#include <string>

using namespace std;
 
int main (){
  string str = "ALPHA";

  //deletes last character of the string
  str.pop_back();
  //deletes next last character of the string
  str.pop_back();

  cout<<str;

  return 0;
}

The output of the above code will be:

ALP

❮ C++ <string> Library