Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | basic string
Revision as of 14:10, 18 October 2024 by Space Mission (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Modifiers
basic_string::pop_back
(DR*)
Search
Operations
Constants
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 
void pop_back();
(constexpr since C++20)

Removes the last character from the string.

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

Contents

[edit] Parameters

(none)

[edit] Return value

(none)

[edit] Complexity

Constant.

[edit] Exceptions

Throws nothing.

[edit] Notes

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

[edit] 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
}

Output:

Before: "Short string!"
After:  "Short string"

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 534 C++98 std::basic_string did not have the member function pop_back() added

[edit] See also

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