Przejdź do głównej zawartości

std::string rend() method

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

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

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

Returns a reverse iterator

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

Attempting to dereference a past-the-end iterator is undefined behaviour

.

zanotuj

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

Parameters

(none)

Return value

Reverse iterator to the first element.

Complexity

Constant - O(1)..

Difference between rend and crend

For a const container c, rend and crend are the same - c.rend() == c.crend()

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

#include <string> 

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

Example

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

int main()
{
std::string s("A man, a plan, a canal: Panama");
{
std::string c;
std::copy(s.rbegin(), s.rend(), std::back_inserter(c));
std::cout << c <<'\n'; // "amanaP :lanac a ,nalp a ,nam A"
}

{
std::string c;
std::copy(s.crbegin(), s.crend(), std::back_inserter(c));
std::cout << c <<'\n'; // "amanaP :lanac a ,nalp a ,nam A"
}
}
Output
amanaP :lanac a ,nalp a ,nam A
amanaP :lanac a ,nalp a ,nam A
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 rend() method

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

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

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

Returns a reverse iterator

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

Attempting to dereference a past-the-end iterator is undefined behaviour

.

zanotuj

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

Parameters

(none)

Return value

Reverse iterator to the first element.

Complexity

Constant - O(1)..

Difference between rend and crend

For a const container c, rend and crend are the same - c.rend() == c.crend()

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

#include <string> 

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

Example

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

int main()
{
std::string s("A man, a plan, a canal: Panama");
{
std::string c;
std::copy(s.rbegin(), s.rend(), std::back_inserter(c));
std::cout << c <<'\n'; // "amanaP :lanac a ,nalp a ,nam A"
}

{
std::string c;
std::copy(s.crbegin(), s.crend(), std::back_inserter(c));
std::cout << c <<'\n'; // "amanaP :lanac a ,nalp a ,nam A"
}
}
Output
amanaP :lanac a ,nalp a ,nam A
amanaP :lanac a ,nalp a ,nam A
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.