In C++, std::string::crbegin() returns a const_reverse_iterator to the last character.
Use it for read-only reverse traversal. Include <string>. (C++11)
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
Pair with crend().
C++
for (auto it = s.crbegin(); it != s.crend(); ++it) { }
Call
What it does
s.crbegin()
Const reverse iterator to the last character.
Cannot assign through crbegin().
Library declarations
C++
const_reverse_iterator crbegin() const noexcept;
Parameters
Parameter
Description
None
crbegin() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
const_reverse_iterator
Read-only reverse iterator to the last character.
C++ Examples
Example 1 — Basic crbegin()
Read the last character.
C++
#include <iostream>
#include <string>
int main() {
std::string s = "end";
std::cout << *s.crbegin() << '\n';
return 0;
}
Output
d
How It Works
crbegin() points at the last character.
Dereferencing reads 'd'.
Assignment through it would not compile.
Example 2 — Const Reverse Walk
Print backwards without modifying.
C++
#include <iostream>
#include <string>
int main() {
std::string s = "dog";
for (auto it = s.crbegin(); it != s.crend(); ++it) {
std::cout << *it;
}
std::cout << '\n';
return 0;
}
Output
god
How It Works
Const reverse iterators walk from back to front.
Characters print in reverse.
The original string stays "dog".
Example 3 — Distance From crbegin
Find how far a letter is from the end.
C++
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string s = "xxAxx";
auto it = std::find(s.crbegin(), s.crend(), 'A');
std::cout << "from end: " << (it - s.crbegin()) << '\n';
return 0;
}
Output
from end: 2
How It Works
Search starts at the last character.
'A' is found two steps from the end.
Const iterators keep the search read-only.
Frequently Asked Questions
Returns a const reverse iterator to the last character.