Difference between revisions of "cpp/iterator/rend"
From cppreference.com
D41D8CD98F (Talk | contribs) m |
(added an example) |
||
Line 52: | Line 52: | ||
| | | | ||
| code= | | code= | ||
+ | #include <iostream> | ||
+ | #include <vector> | ||
+ | #include <iterator> | ||
+ | #include <algorithm> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | std::vector<int> v = {4, 6, -3, 9, 10}; | ||
+ | |||
+ | std::cout << "Array forwards: "; | ||
+ | std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ")); | ||
+ | |||
+ | std::cout << '\n'; | ||
+ | |||
+ | std::cout << "Array backwards: "; | ||
+ | std::copy(v.rbegin(), v.rend(), std::ostream_iterator<int>(std::cout, " ")); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
| output= | | output= | ||
+ | Array forwards: 4 6 -3 9 10 | ||
+ | Array backwards: 10 9 -3 6 4 | ||
}} | }} | ||
Revision as of 04:50, 10 May 2014
Defined in header <iterator>
|
||
template< class C > auto rend( C& c ) -> decltype(c.rend()); |
(1) | (since C++14) |
template< class C > auto rend( const C& c ) -> decltype(c.rend()); |
(1) | (since C++14) |
template< class T, size_t N > reverse_iterator<T*> rend( T (&array)[N] ); |
(2) | (since C++14) |
template< class C > auto crend( const C& c ) -> decltype(std::rend(c)); |
(3) | (since C++14) |
Returns an iterator to the reverse-end of the given container c
or array array
.
1) Returns a possibly const-qualified iterator to the reverse-end of the container
c
.3) Returns a const-qualified iterator to the reverse-end of the container
c
.
Contents |
Parameters
c | - | a container with a rend method
|
array | - | an array of arbitrary type |
Return value
An iterator to the reverse-end of c
or array
Notes
In addition to being included in <iterator>
, std::rend
is guaranteed to become available if any of the following headers are included: <array>
, <deque>
, <forward_list>
, <list>
, <map>
, <regex>
, <set>
, <string>
, <unordered_map>
, <unordered_set>
, and <vector>
.
Specializations
Custom specializations of std::rend
may be provided for classes that do not expose a suitable rend()
member function, yet can be iterated. The following specializations are already provided by the standard library:
(C++14) |
specializes std::rend (function) |
Example
Run this code
#include <iostream> #include <vector> #include <iterator> #include <algorithm> int main() { std::vector<int> v = {4, 6, -3, 9, 10}; std::cout << "Array forwards: "; std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; std::cout << "Array backwards: "; std::copy(v.rbegin(), v.rend(), std::ostream_iterator<int>(std::cout, " ")); return 0; }
Output:
Array forwards: 4 6 -3 9 10 Array 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) |
(C++14) |
returns a reverse iterator to the beginning of a container or array (function template) |
(C++11)(C++14) |
returns an iterator to the beginning of a container or array (function template) |