Skip to main content

std::string rbegin() method

// Non-const version
constexpr iterator end() noexcept;

// Const version
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept;

Returns a reverse iterator

to the first element of the reversed string.
It corresponds to the last element of the non-reversed string.

note

This method doesn't actually reverse the string, it returns an iterator that points to the last element of the string, and which +, -, --, ++ operators have slightly changed implementations.

For example it++ decrements the internal pointer and it-- increments it (so that traversing the container in a reverse order actually works).

If the container is empty, the returned iterator will be equal to rend().

Parameters

(none)

Return value

Reverse iterator to the first element.

Complexity

Constant - O(1)..

Difference between rbegin and crbegin

For a const container c, rbegin and crbegin are the same - c.rbegin() == c.crbegin()

For non-const container of type c they return different iterators:

#include <string>

int main()
{
std::string str = "Hello";
auto it = str.rbegin(); // Type: std::string::reverse_iterator
*it = 'J'; // ✔ Ok
}

Example

Main.cpp
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>

int main()
{
std::string s("Exemplar!");
*s.rbegin() = 'y';
std::cout << s << '\n'; // "Exemplary"

std::string c;
std::copy(s.crbegin(), s.crend(), std::back_inserter(c));
std::cout << c << '\n'; // "yralpmexE"
}
Output
Exemplary
yralpmexE
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::string rbegin() method

// Non-const version
constexpr iterator end() noexcept;

// Const version
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept;

Returns a reverse iterator

to the first element of the reversed string.
It corresponds to the last element of the non-reversed string.

note

This method doesn't actually reverse the string, it returns an iterator that points to the last element of the string, and which +, -, --, ++ operators have slightly changed implementations.

For example it++ decrements the internal pointer and it-- increments it (so that traversing the container in a reverse order actually works).

If the container is empty, the returned iterator will be equal to rend().

Parameters

(none)

Return value

Reverse iterator to the first element.

Complexity

Constant - O(1)..

Difference between rbegin and crbegin

For a const container c, rbegin and crbegin are the same - c.rbegin() == c.crbegin()

For non-const container of type c they return different iterators:

#include <string>

int main()
{
std::string str = "Hello";
auto it = str.rbegin(); // Type: std::string::reverse_iterator
*it = 'J'; // ✔ Ok
}

Example

Main.cpp
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>

int main()
{
std::string s("Exemplar!");
*s.rbegin() = 'y';
std::cout << s << '\n'; // "Exemplary"

std::string c;
std::copy(s.crbegin(), s.crend(), std::back_inserter(c));
std::cout << c << '\n'; // "yralpmexE"
}
Output
Exemplary
yralpmexE
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.