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

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Operations
basic_string::pop_back
(C++11)
Search
Constants
Deduction guides (C++17)
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversion
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Helper classes
 
void pop_back();
(since C++11)
(until C++20)
constexpr void pop_back();
(since C++20)

Removes the last character from the string.

Equivalent to erase(end()-1). The behavior is undefined if the string is empty.

Parameters

(none)

Return value

(none)

Complexity

Constant.

Exceptions

Throws nothing.

Example

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

Output:

before="Short string!"
 after="Short string"

See also

appends a character to the end
(public member function)
removes characters
(public member function)