Namespaces
Variants

std::vector<T,Allocator>:: pop_back

From cppreference.net

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

Entfernt das letzte Element des Containers.

Wenn empty() true ist, ist das Verhalten undefiniert.

(bis C++26)

Wenn empty() true ist:

  • Wenn die Implementierung hardened ist, tritt eine contract violation auf. Zudem ist das Verhalten undefiniert, wenn der Contract-Violation-Handler unter "observe"-Auswertungssemantik zurückkehrt.
  • Wenn die Implementierung nicht hardened ist, ist das Verhalten undefiniert.
(seit C++26)

Iteratoren und Referenzen auf das letzte Element werden ungültig. Der end() Iterator wird ebenfalls ungültig.

Komplexität

Konstante.

Beispiel

#include <vector>
#include <iostream>
namespace stq
{
    template<typename T>
    void println(auto, const T& xz)
    {
        std::cout << '[';
        bool first{true};
        for (const auto& x : xz)
            std::cout << (first ? first = false, "" : ", ") << x;
        std::cout << "]\n";
    }
}
int main()
{
    std::vector<int> numbers{1, 2, 3};
    stq::println("{}", numbers);
    while (not numbers.empty())
    {
        numbers.pop_back();
        stq::println("{}", numbers);
    }
}

Ausgabe:

[1, 2, 3]
[1, 2]
[1]
[]

Siehe auch

fügt ein Element am Ende hinzu
(öffentliche Elementfunktion)