In C++, std::string::crend() marks the end of a read-only reverse walk.
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
Use as the end condition with crbegin().
C++
for (auto it = s.crbegin(); it != s.crend(); ++it) { }
Call
What it does
s.crend()
Const reverse past-the-end iterator.
Never dereference crend().
Library declarations
C++
const_reverse_iterator crend() const noexcept;
Parameters
Parameter
Description
None
crend() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
const_reverse_iterator
Stop marker for const reverse loops.
C++ Examples
Example 1 — Loop to crend()
Print a word backwards.
C++
#include <iostream>
#include <string>
int main() {
std::string s = "on";
for (auto it = s.crbegin(); it != s.crend(); ++it) {
std::cout << *it;
}
std::cout << '\n';
return 0;
}