Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | basic string
m (Add simple example)
m ({{ddcla}})
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{cpp/string/basic_string/title | pop_back}}
+
{{cpp/string/basic_string/title|pop_back}}
 
{{cpp/string/basic_string/navbar}}
 
{{cpp/string/basic_string/navbar}}
{{dcl begin}}
+
{{ddcla|constexpr=c++20|
{{dcl rev multi
+
| since1=c++11 | dcl1=
+
 
void pop_back();
 
void pop_back();
| since2=c++20 | dcl2=
 
constexpr void pop_back();
 
 
}}
 
}}
{{dcl end}}
 
  
 
Removes the last character from the string.
 
Removes the last character from the string.
  
Equivalent to {{c|erase(end()-1)}}. The behavior is undefined if the string is empty.
+
Equivalent to {{c|erase(end() - 1)}}. The behavior is undefined if the string is empty.
  
 
===Parameters===
 
===Parameters===
Line 25: Line 20:
 
===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===
 
{{example
 
{{example
| code=
+
|code=
 
#include <cassert>
 
#include <cassert>
#include <string>
+
#include <iomanip>
 
#include <iostream>
 
#include <iostream>
+
#include <string>
 +
 
 
int main()
 
int main()
 
{
 
{
     std::string str("Short string");
+
     std::string str("Short string!");
     std::cout << "before='" << str << "'\n";
+
     std::cout << "Before: " << std::quoted(str) << '\n';
     assert(str.size() == 12);
+
     assert(str.size() == 13);
+
   
 
     str.pop_back();
 
     str.pop_back();
     std::cout << " after='" << str << "'\n";
+
     std::cout << "After:  " << std::quoted(str) << '\n';
     assert(str.size() == 11);
+
     assert(str.size() == 12);
 +
   
 +
    str.clear();
 +
//  str.pop_back(); // undefined behavior
 
}
 
}
| output=
+
|output=
before='Short string'
+
Before: "Short string!"
  after='Short strin'
+
After: "Short string"
 
}}
 
}}
  
===See also===
+
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=534|std=C++98|before={{lc|std::basic_string}} did not have the member function {{tt|pop_back()}}|after=added}}
 +
{{dr list end}}
  
 +
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/basic_string/dsc push_back}}
+
{{dsc inc|cpp/string/basic_string/dsc push_back}}
{{dsc inc | cpp/string/basic_string/dsc erase}}
+
{{dsc inc|cpp/string/basic_string/dsc erase}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 14:10, 18 October 2024

 
 
 
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]