C++ std::string rend() Function

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

Definition

In C++, std::string::rend() is the stop marker for reverse iteration.

It does not point at a character you can read. 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

Compare reverse iterators with rend().

C++
for (auto it = s.rbegin(); it != s.rend(); ++it) { }
CallWhat it does
s.rend()Reverse past-the-end iterator.

Never dereference rend().

Library declarations
C++
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;

Parameters

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

Return Value

Return ValueDescription
reverse_iteratorStop iterator for reverse loops.

C++ Examples

Example 1 — Stop at rend()

Print characters backwards.

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

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

How It Works

  1. Iteration starts at rbegin().
  2. It ends when it == rend().
  3. Output is reversed.

Example 2 — rbegin Equals rend

Empty string reverse bounds match.

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

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

How It Works

  1. No characters means reverse begin equals reverse end.
  2. The comparison is valid.
  3. Do not dereference either iterator here.

Example 3 — Build a Reversed Copy

Append characters while walking reverse iterators.

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

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

How It Works

  1. We walk from last to first.
  2. Each character is pushed into rev.
  3. The new string is the reverse of the original.

Frequently Asked Questions

The end marker for reverse iteration—do not dereference it.
rbegin() or crbegin().
No. Undefined behavior.
No. Reverse iterators that reach rend have moved past the first character.
Yes: crend().

Next: replace()

Keep going through the string functions. Replace a slice of the string with new characters.

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

7 people found this page helpful