Home C++ Strings Functions rbegin() C++ std::string rbegin() Function 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.
for (auto it = s.rbegin(); it != s.rend(); ++it) {
std::cout << *it;
} Call What it does s.rbegin()Reverse iterator to the last character.
For an empty string, rbegin() == rend(). Do not dereference in that case.
Library declarations reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept; Parameters Parameter Description None rbegin() takes no parameters. Call it with empty brackets.
Return Value Return Value Description reverse_iterator Points at the last character when the string is not empty.
C++ Examples 1. Last char 2. Print reverse 3. Change last Example 1 — Basic rbegin() Print the last character.
#include <iostream>
#include <string>
int main() {
std::string s = "fun";
std::cout << *s.rbegin() << '\n';
return 0;
} How It Works rbegin() points at the last character.Dereferencing yields 'n'. This matches what back() would return. Example 2 — Walk Backwards Print the string in reverse order.
#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 The loop starts at the last character. Each ++it moves toward the front. Output is the reverse spelling. Example 3 — Write Via rbegin() Change the last character through a reverse iterator.
#include <iostream>
#include <string>
int main() {
std::string s = "cat";
*s.rbegin() = 'r';
std::cout << s << '\n';
return 0;
} How It Works rbegin() refers to the last character 't'.Assignment changes it to 'r'. The string becomes "car". Frequently Asked Questions What does rbegin() return? A reverse iterator to the last character.
How do I iterate a string backwards? for (auto it = s.rbegin(); it != s.rend(); ++it).
Is *rbegin() the same as back()? For a non-empty string, yes they refer to the same character.
What about an empty string? rbegin() equals rend(); do not dereference.
What is the const version? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion