Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/make reverse iterator"

From cppreference.com
< cpp‎ | iterator
(+)
 
(Wording update.)
 
(10 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 
{{cpp/title|make_reverse_iterator}}
 
{{cpp/title|make_reverse_iterator}}
 
{{cpp/iterator/navbar}}
 
{{cpp/iterator/navbar}}
{{dcl begin}}
+
{{ddcla|header=iterator|since=c++14|constexpr=c++17|
{{dcl header | iterator}}
+
template< class Iter >
{{dcl| since=c++14 |
+
std::reverse_iterator<Iter> make_reverse_iterator( Iter i );
template <class Iterator>
+
std::reverse_iterator<Iterator> make_reverse_iterator( Iterator i );
+
 
}}
 
}}
{{dcl end}}
 
  
{{tt|make_reverse_iterator}} is a convenience function template that constructs a {{lc|std::reverse_iterator}} for the given iterator {{tt|i}} with the type deduced from the type of the argument.
+
{{tt|make_reverse_iterator}} is a convenience function template that constructs a {{lc|std::reverse_iterator}} for the given iterator {{c|i}} (which must be a {{named req|BidirectionalIterator}}) with the type deduced from the type of the argument.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | i | input iterator to be converted to reverse iterator}}
+
{{par|i|iterator to be converted to reverse iterator}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
A {{lc|std::reverse_iterator}} constructed from {{tt|i}}
+
{{c|std::reverse_iterator<Iter>(i)}}
  
===Possible implementation===
+
===Notes===
{{eq fun | 1=
+
{{feature test macro|__cpp_lib_make_reverse_iterator|std=C++14|value=201402L|{{tt|std::make_reverse_iterator}}}}
template< class Iterator >
+
 
std::reverse_iterator<Iterator> make_reverse_iterator( Iterator i )
+
===Example===
 +
{{example
 +
|code=
 +
#include <algorithm>
 +
#include <iostream>
 +
#include <iterator>
 +
#include <vector>
 +
 
 +
int main()
 
{
 
{
     return std::reverse_iterator<Iterator>(i);
+
     std::vector<int> v{1, 3, 10, 8, 22};
 +
   
 +
    std::sort(v.begin(), v.end());
 +
    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ", "));
 +
    std::cout << '\n';
 +
   
 +
    std::copy(std::make_reverse_iterator(v.end()),
 +
              std::make_reverse_iterator(v.begin()),
 +
              std::ostream_iterator<int>(std::cout, ", "));
 +
    std::cout << '\n';
 
}
 
}
 +
|output=
 +
1, 3, 8, 10, 22,
 +
22, 10, 8, 3, 1,
 
}}
 
}}
 
===Example===
 
{{example}}
 
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/iterator/dsc reverse_iterator}}
+
{{dsc inc|cpp/iterator/dsc reverse_iterator}}
{{dsc inc | cpp/iterator/dsc rbegin}}
+
{{dsc inc|cpp/iterator/dsc rbegin}}
{{dsc inc | cpp/iterator/dsc rend}}
+
{{dsc inc|cpp/iterator/dsc rend}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/iterator/make reverse iterator]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/iterator/make reverse iterator]]
+
[[fr:cpp/iterator/make reverse iterator]]
+
[[it:cpp/iterator/make reverse iterator]]
+
[[ja:cpp/iterator/make reverse iterator]]
+
[[pt:cpp/iterator/make reverse iterator]]
+
[[ru:cpp/iterator/make reverse iterator]]
+
[[zh:cpp/iterator/make reverse iterator]]
+

Latest revision as of 22:33, 4 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)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
Defined in header <iterator>
template< class Iter >
std::reverse_iterator<Iter> make_reverse_iterator( Iter i );
(since C++14)
(constexpr since C++17)

make_reverse_iterator is a convenience function template that constructs a std::reverse_iterator for the given iterator i (which must be a LegacyBidirectionalIterator) with the type deduced from the type of the argument.

Contents

[edit] Parameters

i - iterator to be converted to reverse iterator

[edit] Return value

std::reverse_iterator<Iter>(i)

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_make_reverse_iterator 201402L (C++14) std::make_reverse_iterator

[edit] Example

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
 
int main()
{
    std::vector<int> v{1, 3, 10, 8, 22};
 
    std::sort(v.begin(), v.end());
    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ", "));
    std::cout << '\n';
 
    std::copy(std::make_reverse_iterator(v.end()),
              std::make_reverse_iterator(v.begin()),
              std::ostream_iterator<int>(std::cout, ", "));
    std::cout << '\n';
}

Output:

1, 3, 8, 10, 22,
22, 10, 8, 3, 1,

[edit] See also

iterator adaptor for reverse-order traversal
(class template) [edit]
returns a reverse iterator to the beginning of a container or array
(function template) [edit]
(C++14)
returns a reverse end iterator for a container or array
(function template) [edit]