Namespaces
Variants
Views
Actions

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

From cppreference.com
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
m (Example: +const_...)
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{page template|cpp/iterator/adaptor/constructor|reverse_iterator}}
+
{{cpp/iterator/reverse_iterator/title|reverse_iterator}}
 +
{{cpp/iterator/reverse_iterator/navbar}}
 +
{{dcl begin}}
 +
{{dcla|num=1|constexpr=c++17|
 +
reverse_iterator();
 +
}}
 +
{{dcla|num=2|constexpr=c++17|
 +
explicit reverse_iterator( iterator_type x );
 +
}}
 +
{{dcla|num=3|constexpr=c++17|
 +
template< class U >
 +
reverse_iterator( const reverse_iterator<U>& other );
 +
}}
 +
{{dcl end}}
  
[[de:cpp/iterator/reverse iterator/reverse iterator]]
+
Constructs a new {{tt|reverse_iterator}}.
[[es:cpp/iterator/reverse iterator/reverse iterator]]
+
 
[[fr:cpp/iterator/reverse iterator/reverse iterator]]
+
{|class="wikitable" style="text-align: center;"
[[it:cpp/iterator/reverse iterator/reverse iterator]]
+
!{{nbsp}}Overload{{nbsp}}
[[ja:cpp/iterator/reverse iterator/reverse iterator]]
+
!style="font-weight: normal;"|{{rlpst|/#current}}
[[pt:cpp/iterator/reverse iterator/reverse iterator]]
+
|-
[[ru:cpp/iterator/reverse iterator/reverse iterator]]
+
|{{v|1}}
[[zh:cpp/iterator/reverse iterator/reverse iterator]]
+
|[[cpp/language/value initialization|value-initialized]]
 +
|-
 +
|{{v|2}}
 +
|initialized with {{c|x}}
 +
|-
 +
|{{v|3}}
 +
|{{nbsp}}initialized with {{box|{{c/core|other.}}{{rlpst|/#current}}}}{{nbsp}}
 +
|}
 +
 
 +
@3@ The converting constructor.
 +
{{rrev|since=c++20|
 +
{{cpp/enable if|{{c|std::is_same_v<U, Iter>}} is {{c|false}} and {{c|std::convertible_to<const U&, Iter>}} is modeled}}.
 +
}}
 +
 
 +
===Parameters===
 +
{{par begin}}
 +
{{par|x|iterator to adapt}}
 +
{{par|other|iterator adaptor to copy}}
 +
{{par end}}
 +
 
 +
===Example===
 +
{{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===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/iterator/adaptor/dsc operator{{=}}|reverse_iterator}}
 +
{{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]