Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/rend"

From cppreference.com
< cpp‎ | iterator
m (Synopsis: +{{dcl sep}}, fmt.)
m (Synopsis: ~constexpr=)
 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
{{cpp/iterator/navbar}}
 
{{cpp/iterator/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header|array}}
+
{{cpp/iterator/range access headers}}
{{dcl header|deque}}
+
{{dcla|anchor=no|num=1|since=c++14|constexpr=c++17|
{{dcl header|forward_list}}
+
{{dcl header|iterator}}
+
{{dcl header|list}}
+
{{dcl header|map}}
+
{{dcl header|regex}}
+
{{dcl header|set}}
+
{{dcl sep}}
+
{{dcl header|span|notes={{mark since c++20}}}}
+
{{dcl sep}}
+
{{dcl header|string}}
+
{{dcl sep}}
+
{{dcl header|string_view|notes={{mark since c++17}}}}
+
{{dcl sep}}
+
{{dcl header|unordered_map}}
+
{{dcl header|unordered_set}}
+
{{dcl header|vector}}
+
{{dcl rev multi|num=1
+
|since1=c++14|dcl1=
+
 
template< class C >
 
template< class C >
 
auto rend( C& c ) -> decltype(c.rend());
 
auto rend( C& c ) -> decltype(c.rend());
|since2=c++17|dcl2=
 
template< class C >
 
constexpr auto rend( C& c ) -> decltype(c.rend());
 
 
}}
 
}}
{{dcl rev multi|num=1
+
{{dcla|anchor=no|num=2|since=c++14|constexpr=c++17|
|since1=c++14|dcl1=
+
 
template< class C >
 
template< class C >
 
auto rend( const C& c ) -> decltype(c.rend());
 
auto rend( const C& c ) -> decltype(c.rend());
|since2=c++17|dcl2=
 
template< class C >
 
constexpr auto rend( const C& c ) -> decltype(c.rend());
 
 
}}
 
}}
{{dcl rev multi|num=2
+
{{dcla|anchor=no|num=3|since=c++14|constexpr=c++17|
|since1=c++14|dcl1=
+
 
template< class T, std::size_t N >
 
template< class T, std::size_t N >
 
std::reverse_iterator<T*> rend( T (&array)[N] );
 
std::reverse_iterator<T*> rend( T (&array)[N] );
|since2=c++17|dcl2=
 
template< class T, std::size_t N >
 
constexpr std::reverse_iterator<T*> rend( T (&array)[N] );
 
 
}}
 
}}
{{dcl rev multi|num=3
+
{{dcla|anchor=no|num=4|since=c++14|constexpr=c++17|
|since1=c++14|dcl1=
+
 
template< class T >
 
template< class T >
 
std::reverse_iterator<const T*> rend( std::initializer_list<T> il );
 
std::reverse_iterator<const T*> rend( std::initializer_list<T> il );
|since2=c++17|dcl2=
 
template< class T >
 
constexpr std::reverse_iterator<const T*> rend( std::initializer_list<T> il );
 
 
}}
 
}}
{{dcl rev multi|num=4
+
{{dcla|anchor=no|num=5|since=c++14|constexpr=c++17|
|since1=c++14|dcl1=
+
 
template< class C >
 
template< class C >
 
auto crend( const C& c ) -> decltype(std::rend(c));
 
auto crend( const C& c ) -> decltype(std::rend(c));
|since2=c++17|dcl2=
 
template< class C >
 
constexpr auto crend( const C& c ) -> decltype(std::rend(c));
 
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 64: Line 27:
 
Returns an iterator to the reverse-end of the given range.
 
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,2@ Returns {{c|c.rend()}}, which is typically an iterator one past the reverse-end of the sequence represented by {{c|c}}.
@2@ Returns {{c|std::reverse_iterator<T*>}} to the reverse-end of the array {{tt|array}}.
+
:@1@ If {{tt|C}} is a standard {{named req|Container}}, returns a {{tt|C::reverse_iterator}} object.
@3@ Returns {{c|std::reverse_iterator<const T*>}} to the reverse-end of the {{lc|std::initializer_list}} {{tt|il}}.
+
:@2@ If {{tt|C}} is a standard {{named req|Container}}, returns a {{tt|C::const_reverse_iterator}} object.
@4@ Returns an iterator to the reverse-end of the const-qualified container or view {{tt|c}}.
+
 
 +
@3@ Returns an {{c/core|std::reverse_iterator<T*>}} object to the reverse-end of {{c|array}}.
 +
 
 +
@4@ Returns an {{c/core|std::reverse_iterator<const T*>}} object to the reverse-end of {{c|il}}.
 +
 
 +
@5@ Returns {{c|std::end(c)}}, with {{c|c}} always treated as const-qualified.
 +
@@ If {{tt|C}} is a standard {{named req|Container}}, returns a {{tt|C::const_reverse_iterator}} object.
  
 
{{image|range-rbegin-rend.svg}}
 
{{image|range-rbegin-rend.svg}}
Line 75: Line 44:
 
{{par|c|a container or view with a {{tt|rend}} member function}}
 
{{par|c|a container or view with a {{tt|rend}} member function}}
 
{{par|array|an array of arbitrary type}}
 
{{par|array|an array of arbitrary type}}
{{par|il|an {{tt|initializer_list}}}}
+
{{par|il|an {{c/core|std::initializer_list}}}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
@1@ {{c|c.rend()}}
+
@1,2@ {{c|c.rend()}}
@2@ {{c|std::reverse_iterator<T*>(array)}}
+
@3@ {{c|std::reverse_iterator<T*>(array)}}
@3@ {{c|std::reverse_iterator<const T*>(il.begin())}}
+
@4@ {{c|std::reverse_iterator<const T*>(il.begin())}}
@4@ {{c|c.rend()}}
+
@5@ {{c|c.rend()}}
  
 
{{cpp/impldef exception}}
 
{{cpp/impldef exception}}
Line 90: Line 59:
  
 
{{rrev|since=c++20|
 
{{rrev|since=c++20|
Overloads of {{tt|rend}} found by [[cpp/language/adl|argument-dependent lookup]] can be used to customize the behavior of {{c|std::ranges::rend}} and {{c|std::ranges::crend}}.
+
Overloads of {{tt|rend}} found by [[cpp/language/adl|argument-dependent lookup]] can be used to customize the behavior of {{c/core|std::ranges::rend}} and {{lc|std::ranges::crend}}.
 
}}
 
}}
  
Line 98: Line 67:
 
===Example===
 
===Example===
 
{{example
 
{{example
|
 
 
|code=
 
|code=
 
#include <algorithm>
 
#include <algorithm>
Line 110: Line 78:
 
     std::cout << "C-style array `a` backwards: ";
 
     std::cout << "C-style array `a` backwards: ";
 
     std::copy(std::rbegin(a), std::rend(a), std::ostream_iterator<int>(std::cout, " "));
 
     std::copy(std::rbegin(a), std::rend(a), std::ostream_iterator<int>(std::cout, " "));
 
+
   
 
     auto il = {3, 1, 4};
 
     auto il = {3, 1, 4};
 
     std::cout << "\nstd::initializer_list `il` backwards: ";
 
     std::cout << "\nstd::initializer_list `il` backwards: ";
 
     std::copy(std::rbegin(il), std::rend(il), std::ostream_iterator<int>(std::cout, " "));
 
     std::copy(std::rbegin(il), std::rend(il), std::ostream_iterator<int>(std::cout, " "));
 
+
   
 
     std::vector<int> v{4, 6, -3, 9, 10};
 
     std::vector<int> v{4, 6, -3, 9, 10};
 
     std::cout << "\nstd::vector `v` backwards: ";
 
     std::cout << "\nstd::vector `v` backwards: ";
 
     std::copy(std::rbegin(v), std::rend(v), std::ostream_iterator<int>(std::cout, " "));
 
     std::copy(std::rbegin(v), std::rend(v), std::ostream_iterator<int>(std::cout, " "));
 +
    std::cout << '\n';
 
}
 
}
 
|output=
 
|output=

Latest revision as of 22:11, 1 November 2024

 
 
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 <flat_map>
Defined in header <flat_set>
Defined in header <forward_list>
Defined in header <inplace_vector>
Defined in header <iterator>
Defined in header <list>
Defined in header <map>
Defined in header <regex>
Defined in header <set>
Defined in header <span>
Defined in header <string>
Defined in header <string_view>
Defined in header <unordered_map>
Defined in header <unordered_set>
Defined in header <vector>
template< class C >
auto rend( C& c ) -> decltype(c.rend());
(1) (since C++14)
(constexpr since C++17)
template< class C >
auto rend( const C& c ) -> decltype(c.rend());
(2) (since C++14)
(constexpr since C++17)
template< class T, std::size_t N >
std::reverse_iterator<T*> rend( T (&array)[N] );
(3) (since C++14)
(constexpr since C++17)
template< class T >
std::reverse_iterator<const T*> rend( std::initializer_list<T> il );
(4) (since C++14)
(constexpr since C++17)
template< class C >
auto crend( const C& c ) -> decltype(std::rend(c));
(5) (since C++14)
(constexpr since C++17)

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

1,2) Returns c.rend(), which is typically an iterator one past the reverse-end of the sequence represented by c.
1) If C is a standard Container, returns a C::reverse_iterator object.
2) If C is a standard Container, returns a C::const_reverse_iterator object.
3) Returns an std::reverse_iterator<T*> object to the reverse-end of array.
4) Returns an std::reverse_iterator<const T*> object to the reverse-end of il.
5) Returns std::end(c), with c always treated as const-qualified.
If C is a standard Container, returns a C::const_reverse_iterator object.

range-rbegin-rend.svg

Contents

[edit] Parameters

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

[edit] Return value

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

[edit] Exceptions

May throw implementation-defined exceptions.

[edit] 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 of rend found by argument-dependent lookup can be used to customize the behavior of std::ranges::rend and std::ranges::crend.

(since C++20)

[edit] Notes

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

[edit] Example

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
 
int main()
{
    int a[]{4, 6, -3, 9, 10};
    std::cout << "C-style array `a` 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 `il` backwards: ";
    std::copy(std::rbegin(il), std::rend(il), std::ostream_iterator<int>(std::cout, " "));
 
    std::vector<int> v{4, 6, -3, 9, 10};
    std::cout << "\nstd::vector `v` backwards: ";
    std::copy(std::rbegin(v), std::rend(v), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

Output:

C-style array `a` backwards: 10 9 -3 6 4
std::initializer_list `il` backwards: 4 1 3
std::vector `v` backwards: 10 9 -3 6 4

[edit] 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]