Difference between revisions of "cpp/iterator/move iterator/operator cmp"
From cppreference.com
< cpp | iterator | move iterator
m (Shorten template names. Use {{lc}} where appropriate.) |
m (fmt.) |
||
(4 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{title|1=operator==,!=,<,<=,>,>=,<=>{{petty|(std::move_iterator)}}}} |
+ | {{cpp/iterator/move_iterator/navbar}} | ||
+ | {{dcl begin}} | ||
+ | {{dcl header|iterator}} | ||
+ | {{dcla|num=1|constexpr=c++17|1= | ||
+ | template< class Iter1, class Iter2 > | ||
+ | bool operator==( const std::move_iterator<Iter1>& lhs, | ||
+ | const std::move_iterator<Iter2>& rhs ); | ||
+ | }} | ||
+ | {{dcla|num=2|constexpr=c++17|until=c++20|1= | ||
+ | template< class Iter1, class Iter2 > | ||
+ | bool operator!=( const std::move_iterator<Iter1>& lhs, | ||
+ | const std::move_iterator<Iter2>& rhs ); | ||
+ | }} | ||
+ | {{dcla|num=3|constexpr=c++17| | ||
+ | template< class Iter1, class Iter2 > | ||
+ | bool operator< ( const std::move_iterator<Iter1>& lhs, | ||
+ | const std::move_iterator<Iter2>& rhs ); | ||
+ | }} | ||
+ | {{dcla|num=4|constexpr=c++17|1= | ||
+ | template< class Iter1, class Iter2 > | ||
+ | bool operator<=( const std::move_iterator<Iter1>& lhs, | ||
+ | const std::move_iterator<Iter2>& rhs ); | ||
+ | }} | ||
+ | {{dcla|num=5|constexpr=c++17| | ||
+ | template< class Iter1, class Iter2 > | ||
+ | bool operator> ( const std::move_iterator<Iter1>& lhs, | ||
+ | const std::move_iterator<Iter2>& rhs ); | ||
+ | }} | ||
+ | {{dcla|num=6|constexpr=c++17|1= | ||
+ | template< class Iter1, class Iter2 > | ||
+ | bool operator>=( const std::move_iterator<Iter1>& lhs, | ||
+ | const std::move_iterator<Iter2>& rhs ); | ||
+ | }} | ||
+ | {{dcl|num=7|since=c++20|1= | ||
+ | template< class Iter1, std::three_way_comparable_with<Iter1> Iter2 > | ||
+ | constexpr std::compare_three_way_result_t<Iter1, Iter2> | ||
+ | operator<=>( const std::move_iterator<Iter1>& lhs, | ||
+ | const std::move_iterator<Iter2>& rhs ); | ||
+ | }} | ||
+ | {{dcl end}} | ||
− | + | Compares the underlying iterators of {{c|lhs}} and {{c|rhs}}. | |
− | + | ||
− | + | {{rrev|since=c++20| | |
− | [ | + | @1@ {{cpp/enable if|{{c|1=lhs.base() == rhs.base()}} is well-formed and convertible to {{c/core|bool}}}}. |
− | + | ||
− | + | @3-6@ {{cpp/enable if|plural=yes|{{c|lhs.base() < rhs.base()}} is well-formed and convertible to {{c/core|bool}}}}. | |
− | + | ||
− | + | {{cpp/note synthesized eq}} | |
+ | }} | ||
+ | |||
+ | ===Parameters=== | ||
+ | {{par begin}} | ||
+ | {{par|lhs, rhs|iterator adaptors to compare}} | ||
+ | {{par end}} | ||
+ | |||
+ | ===Return value=== | ||
+ | @1@ {{c|1=lhs.base() == rhs.base()}} | ||
+ | @2@ {{c|1=!(lhs == rhs)}} | ||
+ | @3@ {{c|lhs.base() < rhs.base()}} | ||
+ | @4@ {{c|!(rhs < lhs)}} | ||
+ | @5@ {{c|rhs < lhs}} | ||
+ | @6@ {{c|!(lhs < rhs)}} | ||
+ | @7@ {{c|1=lhs.base() <=> rhs.base()}} | ||
+ | |||
+ | ===Example=== | ||
+ | {{example | ||
+ | |code= | ||
+ | #include <cassert> | ||
+ | #include <iterator> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int a[]{9, 8, 7, 6}; | ||
+ | // │ └───── x, y | ||
+ | // └──────── z | ||
+ | |||
+ | // “x” and “y” are equal, but “x” is greater than “z” | ||
+ | std::move_iterator<int*> | ||
+ | x{std::end(a) - 1}, | ||
+ | y{std::end(a) - 1}, | ||
+ | z{std::end(a) - 2}; | ||
+ | |||
+ | // two-way comparisons | ||
+ | assert( x == y ); | ||
+ | assert(!(x != y)); | ||
+ | assert(!(x < y)); | ||
+ | assert( x <= y ); | ||
+ | assert(!(x == z)); | ||
+ | assert( x != z ); | ||
+ | assert(!(x < z)); | ||
+ | assert(!(x <= z)); | ||
+ | |||
+ | // three-way comparisons | ||
+ | assert( x <=> y == 0 ); | ||
+ | assert(!(x <=> y < 0)); | ||
+ | assert(!(x <=> y > 0)); | ||
+ | assert(!(x <=> z == 0)); | ||
+ | assert(!(x <=> z < 0)); | ||
+ | assert( x <=> z > 0 ); | ||
+ | } | ||
+ | }} | ||
+ | |||
+ | ===See also=== | ||
+ | {{dsc begin}} | ||
+ | {{dsc inc|cpp/iterator/move_iterator/dsc operator cmp2}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 16:17, 2 November 2024
Defined in header <iterator>
|
||
template< class Iter1, class Iter2 > bool operator==( const std::move_iterator<Iter1>& lhs, |
(1) | (constexpr since C++17) |
template< class Iter1, class Iter2 > bool operator!=( const std::move_iterator<Iter1>& lhs, |
(2) | (constexpr since C++17) (until C++20) |
template< class Iter1, class Iter2 > bool operator< ( const std::move_iterator<Iter1>& lhs, |
(3) | (constexpr since C++17) |
template< class Iter1, class Iter2 > bool operator<=( const std::move_iterator<Iter1>& lhs, |
(4) | (constexpr since C++17) |
template< class Iter1, class Iter2 > bool operator> ( const std::move_iterator<Iter1>& lhs, |
(5) | (constexpr since C++17) |
template< class Iter1, class Iter2 > bool operator>=( const std::move_iterator<Iter1>& lhs, |
(6) | (constexpr since C++17) |
template< class Iter1, std::three_way_comparable_with<Iter1> Iter2 > constexpr std::compare_three_way_result_t<Iter1, Iter2> |
(7) | (since C++20) |
Compares the underlying iterators of lhs and rhs.
1) This overload participates in overload resolution only if lhs.base() == rhs.base() is well-formed and convertible to bool.
3-6) These overloads participate in overload resolution only if lhs.base() < rhs.base() is well-formed and convertible to bool.
The |
(since C++20) |
Contents |
[edit] Parameters
lhs, rhs | - | iterator adaptors to compare |
[edit] Return value
1) lhs.base() == rhs.base()
2) !(lhs == rhs)
3) lhs.base() < rhs.base()
4) !(rhs < lhs)
5) rhs < lhs
6) !(lhs < rhs)
7) lhs.base() <=> rhs.base()
[edit] Example
Run this code
#include <cassert> #include <iterator> int main() { int a[]{9, 8, 7, 6}; // │ └───── x, y // └──────── z // “x” and “y” are equal, but “x” is greater than “z” std::move_iterator<int*> x{std::end(a) - 1}, y{std::end(a) - 1}, z{std::end(a) - 2}; // two-way comparisons assert( x == y ); assert(!(x != y)); assert(!(x < y)); assert( x <= y ); assert(!(x == z)); assert( x != z ); assert(!(x < z)); assert(!(x <= z)); // three-way comparisons assert( x <=> y == 0 ); assert(!(x <=> y < 0)); assert(!(x <=> y > 0)); assert(!(x <=> z == 0)); assert(!(x <=> z < 0)); assert( x <=> z > 0 ); }
[edit] See also
compares the underlying iterator and the underlying sentinel (function template) |