C++ std::string rbegin() Function

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

Definition

In C++, std::string::rbegin() returns a reverse iterator to the last character.

Incrementing a reverse iterator moves toward the beginning of the string. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Use with rend() to walk backwards.

C++
for (auto it = s.rbegin(); it != s.rend(); ++it) {
    std::cout << *it;
}
CallWhat it does
s.rbegin()Reverse iterator to the last character.

For an empty string, rbegin() == rend(). Do not dereference in that case.

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_iteratorPoints at the last character when the string is not empty.

C++ Examples

Example 1 — Basic rbegin()

Print the last character.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "fun";
    std::cout << *s.rbegin() << '\n';
    return 0;
}

How It Works

  1. rbegin() points at the last character.
  2. Dereferencing yields 'n'.
  3. This matches what back() would return.

Example 2 — Walk Backwards

Print the string in reverse order.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "abc";
    for (auto it = s.rbegin(); it != s.rend(); ++it) {
        std::cout << *it;
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The loop starts at the last character.
  2. Each ++it moves toward the front.
  3. Output is the reverse spelling.

Example 3 — Write Via rbegin()

Change the last character through a reverse iterator.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "cat";
    *s.rbegin() = 'r';
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. rbegin() refers to the last character 't'.
  2. Assignment changes it to 'r'.
  3. The string becomes "car".

Frequently Asked Questions

A reverse iterator to the last character.
for (auto it = s.rbegin(); it != s.rend(); ++it).
For a non-empty string, yes they refer to the same character.
rbegin() equals rend(); do not dereference.
crbegin() returns a const_reverse_iterator.

Next: rend()

Keep going through the string functions. Reverse iterator one before the first character—reverse stop mark.

rend() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

15 people found this page helpful