Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/rend"

From cppreference.com
< cpp‎ | iterator
(+ ranges)
m (Use 'range'.)
Line 58: Line 58:
 
{{dcl end}}
 
{{dcl end}}
  
Returns an iterator to the reverse-end of the given container or view {{tt|c}} or array {{tt|array}}.
+
Returns an iterator to the reverse-end of the given range.
  
 
@1@ Returns an iterator to the reverse-end of the possibly const-qualified container or view {{tt|c}}.
 
@1@ Returns an iterator to the reverse-end of the possibly const-qualified container or view {{tt|c}}.

Revision as of 23:32, 3 April 2021

 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
rendcrend
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
Defined in header <array>
Defined in header <deque>
Defined in header <forward_list>
Defined in header <iterator>
Defined in header <list>
Defined in header <map>
Defined in header <regex>
Defined in header <set>
Defined in header <span>
(since C++20)
Defined in header <string>
Defined in header <string_view>
(since C++17)
Defined in header <unordered_map>
Defined in header <unordered_set>
Defined in header <vector>
(1)
template< class C >
auto rend( C& c ) -> decltype(c.rend());
(since C++14)
(until C++17)
template< class C >
constexpr auto rend( C& c ) -> decltype(c.rend());
(since C++17)
(1)
template< class C >
auto rend( const C& c ) -> decltype(c.rend());
(since C++14)
(until C++17)
template< class C >
constexpr auto rend( const C& c ) -> decltype(c.rend());
(since C++17)
(2)
template< class T, std::size_t N >
std::reverse_iterator<T*> rend( T (&array)[N] );
(since C++14)
(until C++17)
template< class T, std::size_t N >
constexpr std::reverse_iterator<T*> rend( T (&array)[N] );
(since C++17)
(3)
template< class T >
std::reverse_iterator<const T*> rend( std::initializer_list<T> il );
(since C++14)
(until C++17)
template< class T >
constexpr std::reverse_iterator<const T*> rend( std::initializer_list<T> il );
(since C++17)
(4)
template< class C >
auto crend( const C& c ) -> decltype(std::rend(c));
(since C++14)
(until C++17)
template< class C >
constexpr auto crend( const C& c ) -> decltype(std::rend(c));
(since C++17)

Returns an iterator to the reverse-end of the given range.

1) Returns an iterator to the reverse-end of the possibly const-qualified container or view c.
2) Returns std::reverse_iterator<T*> to the reverse-end of the array array.
3) Returns std::reverse_iterator<const T*> to the reverse-end of the std::initializer_list il.
4) Returns an iterator to the reverse-end of the const-qualified container or view c.

range-rbegin-rend.svg

Contents

Parameters

c - a container or view with a rend member function
array - an array of arbitrary type
il - an initializer_list

Return value

1) c.rend()
2) std::reverse_iterator<T*>(array)
3) std::reverse_iterator<const T*>(il.begin())
4) c.rend()

Exceptions

May throw implementation-defined exceptions.

Overloads

Custom overloads of rend may be provided for classes and enumerations that do not expose a suitable rend() member function, yet can be iterated.

Overloads found by argument dependent lookup can be used to customize the behavior of std::ranges::rend and std::ranges::crend.

(since C++20)

Notes

The overload for std::initializer_list is necessary because it does not have a member function rend.

Example

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
 
int main()
{
    int a[] = {4, 6, -3, 9, 10};
    std::cout << "Array backwards: ";
    std::copy(std::rbegin(a), std::rend(a), std::ostream_iterator<int>(std::cout, " "));
 
    auto il = { 3, 1, 4 };
    std::cout << "\nstd::initializer_list backwards: ";
    std::copy(std::rbegin(il), std::rend(il), std::ostream_iterator<int>(std::cout, " "));
 
    std::cout << "\nVector backwards: ";
    std::vector<int> v = {4, 6, -3, 9, 10};
    std::copy(std::rbegin(v), std::rend(v), std::ostream_iterator<int>(std::cout, " "));
}

Output:

Array backwards: 10 9 -3 6 4 
std::initializer_list backwards: 4 1 3 
Vector backwards: 10 9 -3 6 4

See also

(C++11)(C++14)
returns an iterator to the end of a container or array
(function template) [edit]
returns a reverse iterator to the beginning of a container or array
(function template) [edit]
(C++11)(C++14)
returns an iterator to the beginning of a container or array
(function template) [edit]
returns a reverse end iterator to a range
(customization point object)[edit]
returns a reverse end iterator to a read-only range
(customization point object)[edit]