Namespaces
Variants
Views
Actions

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

From cppreference.com
(fmt)
m (Example: +const_...)
 
(9 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{cpp/title | n = reverse_iterator:: | reverse_iterator}}
+
{{cpp/iterator/reverse_iterator/title|reverse_iterator}}
 
{{cpp/iterator/reverse_iterator/navbar}}
 
{{cpp/iterator/reverse_iterator/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list item | num=1 |  
+
{{dcla|num=1|constexpr=c++17|
 
reverse_iterator();
 
reverse_iterator();
 
}}
 
}}
{{ddcl list item | num=2 |
+
{{dcla|num=2|constexpr=c++17|
explicit reverse_iterator( Iterator x );  
+
explicit reverse_iterator( iterator_type x );
 
}}
 
}}
{{ddcl list item | num=3 |
+
{{dcla|num=3|constexpr=c++17|
template< class U >  
+
template< class U >
 
reverse_iterator( const reverse_iterator<U>& other );
 
reverse_iterator( const reverse_iterator<U>& other );
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
Constructs a new iterator
+
Constructs a new {{tt|reverse_iterator}}.
  
{{li begin}}
+
{|class="wikitable" style="text-align: center;"
{{li|1}} Default constructor. {{tt|current}} is value-initialized. This makes sense only if operations on an value-initialized {{tt|Iterator}} make also sense.
+
!{{nbsp}}Overload{{nbsp}}
 +
!style="font-weight: normal;"|{{rlpst|/#current}}
 +
|-
 +
|{{v|1}}
 +
|[[cpp/language/value initialization|value-initialized]]
 +
|-
 +
|{{v|2}}
 +
|initialized with {{c|x}}
 +
|-
 +
|{{v|3}}
 +
|{{nbsp}}initialized with {{box|{{c/core|other.}}{{rlpst|/#current}}}}{{nbsp}}
 +
|}
  
{{li|2}} {{tt|current}} is initialized with {{tt|x}}.
+
@3@ The converting constructor.
 
+
{{rrev|since=c++20|
{{li|3}} Copy constructor. The underlying iterator is initialized with that of {{tt|other}}.
+
{{cpp/enable if|{{c|std::is_same_v<U, Iter>}} is {{c|false}} and {{c|std::convertible_to<const U&, Iter>}} is modeled}}.
{{li end}}
+
}}
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | x | iterator to adapt}}
+
{{par|x|iterator to adapt}}
{{param list item | other | iterator adaptor to copy}}
+
{{par|other|iterator adaptor to copy}}
{{param list end}}
+
{{par end}}
  
 
===Example===
 
===Example===
{{todo}}
+
{{example
 +
|code=
 +
#include <cassert>
 +
#include <concepts>
 +
#include <iterator>
 +
#include <vector>
 +
 
 +
int main()
 +
{
 +
    std::vector v{0, 1, 2, 3, 4};
 +
 
 +
    using ReverseIt = std::reverse_iterator<std::vector<int>::const_iterator>;
 +
 
 +
    ReverseIt i1; // overload (1)
 +
    i1 = v.crbegin();
 +
    assert(*i1 == 4);
 +
 
 +
    ReverseIt i2(i1); // overload (2)
 +
    assert(i2[0] == 4);
 +
 
 +
    int x[]{1, 2, 3};
 +
    auto i3 = std::reverse_iterator<int*>(x + std::size(x)); // overload (1)
 +
    i3[0] = -3;
 +
    assert(x[2] == -3);
 +
    std::reverse_iterator<int const*> i4(i3); // overload (3): int => const int
 +
    static_assert(std::convertible_to<decltype(i3)::value_type,
 +
                                      decltype(i4)::value_type>);
 +
    // i4[0] = 5; // Error: assignment of read-only location
 +
}
 +
}}
 +
 
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=235|std=C++98|before=the effect of overload {{vl|1}} was not specified|after=specified}}
 +
{{dr list item|wg=lwg|dr=1012|std=C++98|before=overload {{vl|1}} default-initialized {{rlpst|/#current}}|after=it is value-initialized}}
 +
{{dr list item|wg=lwg|dr=3435|std=C++20|before=overload {{vl|3}} was not constrained|after=constrained}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list mem fun | cpp/iterator/reverse_iterator/operator{{=}} | replaces a reverse_iterator }}
+
{{dsc inc|cpp/iterator/adaptor/dsc operator{{=}}|reverse_iterator}}
{{dcl list end}}
+
{{dsc inc|cpp/iterator/dsc make_reverse_iterator}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 19:39, 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)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
 
reverse_iterator();
(1) (constexpr since C++17)
explicit reverse_iterator( iterator_type x );
(2) (constexpr since C++17)
template< class U >
reverse_iterator( const reverse_iterator<U>& other );
(3) (constexpr since C++17)

Constructs a new reverse_iterator.

 Overload  current
(1) value-initialized
(2) initialized with x
(3)  initialized with other.current 
3) The converting constructor.

This overload participates in overload resolution only if std::is_same_v<U, Iter> is false and std::convertible_to<const U&, Iter> is modeled.

(since C++20)

Contents

[edit] Parameters

x - iterator to adapt
other - iterator adaptor to copy

[edit] Example

#include <cassert>
#include <concepts>
#include <iterator>
#include <vector>
 
int main()
{
    std::vector v{0, 1, 2, 3, 4};
 
    using ReverseIt = std::reverse_iterator<std::vector<int>::const_iterator>;
 
    ReverseIt i1; // overload (1)
    i1 = v.crbegin();
    assert(*i1 == 4);
 
    ReverseIt i2(i1); // overload (2)
    assert(i2[0] == 4);
 
    int x[]{1, 2, 3};
    auto i3 = std::reverse_iterator<int*>(x + std::size(x)); // overload (1)
    i3[0] = -3;
    assert(x[2] == -3);
    std::reverse_iterator<int const*> i4(i3); // overload (3): int => const int
    static_assert(std::convertible_to<decltype(i3)::value_type,
                                      decltype(i4)::value_type>);
    // i4[0] = 5; // Error: assignment of read-only location
}

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 235 C++98 the effect of overload (1) was not specified specified
LWG 1012 C++98 overload (1) default-initialized current it is value-initialized
LWG 3435 C++20 overload (3) was not constrained constrained

[edit] See also

assigns another iterator adaptor
(public member function) [edit]
creates a std::reverse_iterator of type inferred from the argument
(function template) [edit]