Przejdź do głównej zawartości

std::string_view rbegin() method

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

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

Returns a reverse iterator

to the first element of the reversed view.

It corresponds to the last element of the original view.

zanotuj

This method doesn't actually reverse the view, it returns an iterator that points to the last 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 rend().

Parameters

(none)

Return value

Reverse iterator to the first element.

Complexity

Constant - O(1).

Difference between rbegin and crbegin

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

#include <string_view>

int main()
{
std::string_view str = "Hello";
auto it = str.crbegin(); // Type: std::string_view::reverse_const_iterator
*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(), std::next(str_view.rbegin(), 3), out_it);
*out_it = '\n';

std::copy(str_view.crbegin(), std::next(str_view.crbegin(), 3), out_it);
*out_i
}
Output
fed
fed
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 rbegin() method

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

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

Returns a reverse iterator

to the first element of the reversed view.

It corresponds to the last element of the original view.

zanotuj

This method doesn't actually reverse the view, it returns an iterator that points to the last 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 rend().

Parameters

(none)

Return value

Reverse iterator to the first element.

Complexity

Constant - O(1).

Difference between rbegin and crbegin

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

#include <string_view>

int main()
{
std::string_view str = "Hello";
auto it = str.crbegin(); // Type: std::string_view::reverse_const_iterator
*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(), std::next(str_view.rbegin(), 3), out_it);
*out_it = '\n';

std::copy(str_view.crbegin(), std::next(str_view.crbegin(), 3), out_it);
*out_i
}
Output
fed
fed
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.