Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::vector::back — access the last element

Use `back()` to read or change the last element of a non-empty vector. Know when the result becomes invalid and why extracting an element from a heap takes two separate steps.

Version: ISO C++23; implementation support may vary

Syntax

#include <vector>

// Member declarations of std::vector<T, Allocator> in C++23
constexpr reference back();
constexpr const_reference back() const;

std::vector::back is a no-argument member function from <vector>. It accesses the last element in constant time without removing it or changing size(). For ordinary std::vector<T>, the constexpr overloads return T& or, for a const vector, const T&. Thus v.back() = 3 changes the element in a std::vector<int>. In the std::vector<bool> specialization, the non-const overload returns a proxy object by value, and the const overload returns bool by value. For that specialization, bool saved = v.back(); saves the value, not a proxy.

In C++23, calling back() on an empty vector is undefined behavior: no exception or diagnostic is guaranteed. If the vector might be empty, check !v.empty() first. reserve() can increase capacity, but it does not add elements or change size(). On a non-empty vector, back() accesses the same element as *std::prev(v.end()); v.end() itself is a past-the-end iterator and must not be dereferenced.

A saved reference does not track the last position. If push_back() does not reallocate, the reference still refers to the original element, which is no longer last. Reallocation invalidates element references; erasing the referenced element or destroying the vector also makes the reference unusable. If a value must survive erasure, copy it first, for example with int saved = v.back(); for a vector of int.

The last element is not necessarily the minimum or maximum. std::minmax_element(v.begin(), v.end()) returns a pair of iterators: first and second refer to the minimum and maximum, respectively; for an empty range, both equal v.end() and must not be dereferenced.

On a valid non-empty heap, std::pop_heap(v.begin(), v.end()) moves the old root to the last position and leaves the remaining prefix as a heap, but does not change the vector's size. Read the moved value with back(), then erase the element with pop_back(), which returns nothing.

Examples

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v;
    v.reserve(2);
    if (v.empty()) {
        std::cout << "empty\n";
    }

    v.push_back(7);
    v.push_back(2);
    v.back() = 3;
    const std::vector<int>& read_only = v;
    std::cout << read_only.back() << '\n';

    std::make_heap(v.begin(), v.end());
    if (!v.empty()) {
        std::pop_heap(v.begin(), v.end());
        const int removed = v.back();
        v.pop_back();
        std::cout << removed << '\n';
    }
    if (!v.empty()) {
        std::cout << v.back() << '\n';
    }
}

Result

empty
3
7
3

After `reserve(2)`, the vector is still empty. Assignment through `back()` changes `2` to `3`, and reading through the `const` reference prints `3`. After `pop_heap()`, the code copies `7` before erasing the element; `3` remains in the vector.

Related lessons

  • C++ · Lesson 30 · Advanced · Version: 3
  • C++ · Lesson 34 · Advanced · Version: 3

Sources