Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/pop back"

From cppreference.com
< cpp‎ | string‎ | basic string
(Added LWG issue #534 DR (part 3/3).)
(Added a note about the libstdc++ conformance of LWG 534.)
Line 25: Line 25:
 
===Exceptions===
 
===Exceptions===
 
Throws nothing.
 
Throws nothing.
 +
 +
===Notes===
 +
In libstdc++, {{tt|pop_back()}} is [https://gcc.gnu.org/onlinedocs/libstdc++/manual/strings.html#strings.string.shrink not available] in C++98 mode.
  
 
===Example===
 
===Example===

Revision as of 01:22, 31 May 2023

 
 
 
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();
(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.

Contents

Parameters

(none)

Return value

(none)

Complexity

Constant.

Exceptions

Throws nothing.

Notes

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

Example

#include <cassert>
#include <string>
#include <iomanip>
#include <iostream>
 
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"

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

See also

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