C++ std::array rbegin() Function

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

Definition

In C++, std::array::rbegin() returns a reverse iterator. It starts on the last element, and ++ moves it toward the first element.

Because it is not const, you can also change elements through it. Use it with rend() to walk the array backwards. Include <array> to use it.

This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.

Syntax

Call rbegin with empty brackets. It takes no arguments.

C++
auto it = nums.rbegin();
*it = 50;
CallWhat it does
nums.rbegin()Returns a reverse iterator that points to the last element.
*(nums.rbegin() + 1)Reads the second element from the end.

If you only need to read, crbegin() is the read-only version.

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 that points to the last element. On a const array it is a const_reverse_iterator. For std::array<T, 0> it equals rend().

C++ Examples

Example 1 — Print Backwards

Count down from the last element to the first.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 5> nums = {1, 2, 3, 4, 5};

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

How It Works

  1. rbegin() points to the last element, 5.
  2. ++it steps one place toward the front each time.
  3. When it reaches rend() the loop stops, after 1 is printed.

Example 2 — Change the Last Element

A non-const reverse iterator can write to the element.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 3> nums = {10, 20, 30};

    *nums.rbegin() = 50;

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

How It Works

  1. *nums.rbegin() is the last element, so it can be assigned a value.
  2. Only 30 is replaced, and it becomes 50.
  3. The range-based loop prints in the normal front-to-back order, so the change appears at the end.

Example 3 — Reach the Second-Last Element

Add a number to a reverse iterator to move backwards through the array.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 4> nums = {10, 20, 30, 40};

    std::cout << "Last: " << *nums.rbegin() << '\n';
    std::cout << "Second last: " << *(nums.rbegin() + 1) << '\n';
    return 0;
}

How It Works

  1. *nums.rbegin() reads the last value, 40.
  2. nums.rbegin() + 1 moves one place toward the front.
  3. That place holds 30, so reverse iterators make "counting from the end" easy.

Frequently Asked Questions

It returns a reverse iterator that points to the last element. Using ++ on it moves toward the first element.
back() returns the last element itself. rbegin() returns an iterator that starts at the last element and can walk through the whole array backwards.
Yes, on a non-const array. *nums.rbegin() = 5; changes the last element.
No. Use ++. A reverse iterator already moves in the backward direction when you add.
They point to the same element, but rbegin() is a reverse iterator. It walks backwards with ++, while end() - 1 is a normal iterator that walks forward.

Next: rend()

Get a reverse iterator past the first element with std::array.

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.

11 people found this page helpful