C++ std::string crbegin() Function

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

Definition

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) { }
CallWhat 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

ParameterDescription
Nonecrbegin() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
const_reverse_iteratorRead-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;
}

How It Works

  1. crbegin() points at the last character.
  2. Dereferencing reads 'd'.
  3. 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;
}

How It Works

  1. Const reverse iterators walk from back to front.
  2. Characters print in reverse.
  3. 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;
}

How It Works

  1. Search starts at the last character.
  2. 'A' is found two steps from the end.
  3. Const iterators keep the search read-only.

Frequently Asked Questions

Returns a const reverse iterator to the last character.
C++11.
No.
crend().
When the string is empty.

Next: crend()

Keep going through the string functions. const reverse past-the-end iterator.

crend() 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.

13 people found this page helpful