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

#include <iostream>
#include <string>

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

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

  cout<<MyString;

  return 0;
}

The output of the above code will be:

ALP

❮ C++ <string> Library

5