Home

rbegin

rbegin is a reverse iterator access function in the C++ Standard Library. It provides a means to access the last element of a container and iterate in reverse order. The concept is the opposite of begin and is complemented by rend, which marks the end of the reverse sequence. When used, rbegin returns an iterator that moves from the last element toward the first; for empty containers, rbegin equals rend.

Types and overloads: For non-const objects, rbegin returns a reverse_iterator that wraps the container’s normal iterator.

Usage: rbegin is commonly used in explicit reverse loops. Example:

std::vector<int> v{1, 2, 3};

for (auto it = v.rbegin(); it != v.rend(); ++it) {

// use *it

}

You can also use the non-member forms:

for (auto it = std::rbegin(v); it != std::rend(v); ++it) { ... }

rbegin and rend also apply to arrays and other standard sequence types, enabling uniform reverse traversal.

Relation to other iterators: rbegin yields a reverse_iterator that adapts a normal iterator, reversing the direction

For
const
objects,
the
member
function
rbegin()
const
returns
a
const_reverse_iterator.
The
corresponding
end
of
the
reverse
range
is
rend()
(and
rend()
const).
In
addition
to
member
functions,
the
standard
library
provides
non-member
overloads
std::rbegin
and
std::rend
(from
<iterator>)
that
work
with
containers
and
arrays
and
return
reverse_iterator
or
const_reverse_iterator
as
appropriate.
There
are
also
std::crbegin
and
std::crend
for
const
reverse
iteration.
of
traversal.
It
is
part
of
the
iteration
tools
alongside
begin,
end,
and
their
reverse
counterparts.