Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | basic string
(P0980R1)
m (Add simple example)
Line 25: Line 25:
 
===Exceptions===
 
===Exceptions===
 
Throws nothing.
 
Throws nothing.
 +
 +
===Example===
 +
{{example
 +
| code=
 +
#include <cassert>
 +
#include <string>
 +
#include <iostream>
 +
 +
int main()
 +
{
 +
    std::string str("Short string");
 +
    std::cout << "before='" << str << "'\n";
 +
    assert(str.size() == 12);
 +
 +
    str.pop_back();
 +
    std::cout << " after='" << str << "'\n";
 +
    assert(str.size() == 11);
 +
}
 +
| output=
 +
before='Short string'
 +
after='Short strin'
 +
}}
  
 
===See also===
 
===See also===

Revision as of 14:34, 18 January 2022

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

Contents

Parameters

(none)

Return value

(none)

Complexity

Constant.

Exceptions

Throws nothing.

Example

#include <cassert>
#include <string>
#include <iostream>
 
int main()
{
    std::string str("Short string");
    std::cout << "before='" << str << "'\n";
    assert(str.size() == 12);
 
    str.pop_back();
    std::cout << " after='" << str << "'\n";
    assert(str.size() == 11);
}

Output:

before='Short string'
 after='Short strin'

See also

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