std::basic_string<CharT,Traits,Allocator>::pop_back

Removes the last character from the string.

# Declarations

void pop_back();

(constexpr since C++20)

# Return value

(none)

# Notes

In libstdc++, pop_back() is not available in C++98 mode.

# Example

#include <cassert>
#include <iomanip>
#include <iostream>
#include <string>
 
int main()
{
    std::string str("Short string!");
    std::cout << "Before: " << std::quoted(str) << '\n';
    assert(str.size() == 13);
 
    str.pop_back();
    std::cout << "After:  " << std::quoted(str) << '\n';
    assert(str.size() == 12);
 
    str.clear();
//  str.pop_back(); // undefined behavior
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 534C++98std::basic_string did not have the member function pop_back()added

# See also