C++ std::vector back() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::vector

Definition

In C++, std::vector::back() returns the last element of a std::vector. You do not need to calculate size() - 1 yourself.

It returns a reference, so you can read the value or change it. The vector must have at least one element. Include <vector> to use it.

This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.

Syntax

Call back with empty brackets. It takes no arguments.

C++
v.back();
v.back() = 99;
CallWhat it does
v.back()Reads the last element.
v.back() = 99Replaces the last element with a new value.

The first element has a matching function: front(). To remove the last element, use pop_back().

Library declarations
C++
reference       back();
const_reference back() const;

Parameters

ParameterDescription
Noneback() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
ReferenceA reference to the last element, which is the same as d[d.size() - 1].

C++ Examples

Example 1 — Basic back()

Print the last element of a small vector.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {3, 6, 9, 12};

    std::cout << "Last element: " << v.back() << '\n';
    return 0;
}

How It Works

  1. The vector holds four numbers, and 12 is stored at the back end.
  2. back() returns a reference to that last slot.
  3. cout prints the value, so the output is 12.

Example 2 — Change the Last Element

Assign to back() to replace the last value.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {1, 2, 3};

    v.back() = 99;

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. back() is a reference, so it can sit on the left side of =.
  2. Only the last slot changes from 3 to 99. The size of the vector stays the same.
  3. The range-based for loop prints every element, which shows the new value.

Example 3 — back() with pop_back()

pop_back() returns nothing, so read the value with back() first.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {4, 8, 15, 16};

    while (!v.empty()) {
        std::cout << v.back() << ' ';
        v.pop_back();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. !d.empty() makes sure the vector has an element before back() is called.
  2. back() reads the last value, then pop_back() removes it, so the next last value moves into place.
  3. The numbers appear in reverse order, and the loop ends when the vector is empty. This is how a stack works.

Frequently Asked Questions

It returns a reference to the last element of the vector. That is the element at index size() - 1.
No. Calling back() on an empty vector is undefined behavior. Check empty() first.
back() gives you the last element itself. end() gives an iterator that points just after the last element. You must not dereference end().
No. back() only looks at the last element. Use pop_back() to remove it. pop_back() does not return the value, so call back() first.
Yes, for a non-empty vector. back() is shorter and easier to read.

Next: begin()

Get an iterator to the first element with std::vector.

begin() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful