C++ std::vector rbegin() Function

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

Definition

In C++, std::vector::rbegin() returns a reverse iterator that points to the last element of the vector. Moving it with ++ goes backwards, towards the first element.

Use it with rend() to walk through the vector from back to front. It can also change elements. 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 rbegin with empty brackets. It takes no arguments.

C++
auto it = v.rbegin();
*it = 99;   // changes the last element
CallWhat it does
v.rbegin()Returns a reverse iterator that starts on the last element.
*v.rbegin() = 99Changes the last element through the reverse iterator.

For a read-only version, use crbegin().

Library declarations
C++
reverse_iterator       rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;

Parameters

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

Return Value

Return ValueDescription
Reverse iteratorA reverse_iterator to the last element. On a const vector it is a const_reverse_iterator.
Empty vectorFor an empty vector, rbegin() equals rend().

C++ Examples

Example 1 — Loop Backwards with rbegin()

A reverse loop starts at rbegin() and ends at rend().

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

int main() {
    std::vector<int> v = {10, 20, 30};

    for (auto it = v.rbegin(); it != v.rend(); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. rbegin() starts on 30, the last element.
  2. Each ++it moves one place towards the front, so the order is 30 20 10.
  3. The loop stops at rend(). The vector is not reordered.

Example 2 — Change Values Through rbegin()

A reverse iterator that is not const can write to the elements.

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

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

    auto it = v.rbegin();
    *it = 99;
    ++it;
    *it += 1;

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

How It Works

  1. *it = 99 replaces the last element, which was 3.
  2. ++it moves the reverse iterator one place towards the front, onto 2.
  3. *it += 1 changes that element to 3. The vector is now 1 3 99.

Example 3 — Make a Reversed Copy

Pass the reverse iterator pair to a constructor to build a reversed vector.

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

int main() {
    std::vector<int> v = {1, 2, 3, 4};
    std::vector<int> rev(v.rbegin(), v.rend());

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

How It Works

  1. rev(d.rbegin(), d.rend()) copies the elements in the order that the reverse iterators visit them.
  2. That order is back to front, so rev becomes 4 3 2 1.
  3. The original vector d is not changed. It is still 1 2 3 4.

Frequently Asked Questions

It returns a reverse iterator that points to the last element of the vector. Incrementing it moves towards the first element.
end() points after the last element and cannot be read. rbegin() points to the last element and can be read. They also move in opposite directions.
No. The vector stays the same. Only the direction of the walk changes.
Yes, on a non-const vector. Use crbegin() if you want a read-only reverse iterator.
rend(). It marks the place just before the first element, so it is the end marker for reverse walking.

Next: rend()

Get the reverse end marker, just before the first element with std::vector.

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

14 people found this page helpful