Skip to main content

std::string_view rend() method

// Const version only
constexpr const iterator rend() const noexcept;

// Const version only
constexpr const_iterator crend() const noexcept;

Returns a reverse iterator

to the last element of the reversed view.

It corresponds to the element preceding the first element of the original view.

Undefined Behavior

Attempting to dereference a past the end iterator is undefined behaviour

.

note

This method doesn't actually reverse the view, it just returns an iterator that points to the element before the first element of the view, 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 rbegin().

Parameters

(none)

Return value

Reverse iterator to the first element.

Complexity

Constant - O(1)..

Difference between rend and crend

Unlike for other containers, like std::string or std::vector, rend and crend both return the same iterator.

#include <string_view> 

int main()
{
std::string_view str = "Hello";
auto it = str.crend(); // Type: std::string_view::reverse_const_iterator
*std::prev(it) = 'J'; // ❌ Error!
}

Example

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

int main()
{
std::ostream_iterator<char> out_it(std::cout);
std::string_view str_view("abcdef");

std::copy(str_view.rbegin(), str_view.rend(), out_it);
*out_it = '\n';

std::copy(str_view.crbegin(), str_view.crend(), out_it);
*out_it = '\n';
}
Output
fedcba
fedcba
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_view rend() method

// Const version only
constexpr const iterator rend() const noexcept;

// Const version only
constexpr const_iterator crend() const noexcept;

Returns a reverse iterator

to the last element of the reversed view.

It corresponds to the element preceding the first element of the original view.

Undefined Behavior

Attempting to dereference a past the end iterator is undefined behaviour

.

note

This method doesn't actually reverse the view, it just returns an iterator that points to the element before the first element of the view, 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 rbegin().

Parameters

(none)

Return value

Reverse iterator to the first element.

Complexity

Constant - O(1)..

Difference between rend and crend

Unlike for other containers, like std::string or std::vector, rend and crend both return the same iterator.

#include <string_view> 

int main()
{
std::string_view str = "Hello";
auto it = str.crend(); // Type: std::string_view::reverse_const_iterator
*std::prev(it) = 'J'; // ❌ Error!
}

Example

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

int main()
{
std::ostream_iterator<char> out_it(std::cout);
std::string_view str_view("abcdef");

std::copy(str_view.rbegin(), str_view.rend(), out_it);
*out_it = '\n';

std::copy(str_view.crbegin(), str_view.crend(), out_it);
*out_it = '\n';
}
Output
fedcba
fedcba
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.