Difference between revisions of "Template:cpp/container/operator cmp"
m (-2) |
m (→Example: +<compare>) |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | {{title|1=operator==,!=,<,<=,>,>=,<=>{{ | + | {{#vardefine:cont|{{{1|vector}}}}}<!-- |
− | {{cpp/container/{{ | + | -->{{title|1=operator==,!=,<,<=,>,>=,<=>{{petty|(std::{{#var:cont}})}}}} |
− | {{cpp/container/get_types|{{ | + | {{cpp/container/{{#var:cont}}/navbar}} |
+ | {{cpp/container/get_types|{{#var:cont}}}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header|{{cpp/container/get_header|{{ | + | {{dcl header|{{cpp/container/get_header|{{#var:cont}}}}}} |
− | {{#switch:{{ | + | {{#switch:{{#var:cont}} |
− | {{dcl | + | |array|vector= |
− | | | + | {{dcl|num=1|since={{cpp/std|{{#var:cont}}}}|notes={{mark constexpr since c++20}}|1= |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
template< {{#var:types}} > | template< {{#var:types}} > | ||
− | + | bool operator==( const std::{{#var:cont}}<{{#var:types_short}}>& lhs, | |
− | + | const std::{{#var:cont}}<{{#var:types_short}}>& rhs ); | |
}} | }} | ||
|#default= | |#default= | ||
− | {{dcl|num=1|since={{cpp/std|{{ | + | {{dcl|num=1|since={{cpp/std|{{#var:cont}}}}|1= |
template< {{#var:types}} > | template< {{#var:types}} > | ||
− | bool operator==( const std::{{ | + | bool operator==( const std::{{#var:cont}}<{{#var:types_short}}>& lhs, |
− | const std::{{ | + | const std::{{#var:cont}}<{{#var:types_short}}>& rhs ); |
}} | }} | ||
}} | }} | ||
− | {{dcl|num=2|since={{cpp/std|{{ | + | {{dcl|num=2|since={{cpp/std|{{#var:cont}}}}|until=c++20|1= |
template< {{#var:types}} > | template< {{#var:types}} > | ||
− | bool operator!=( const std::{{ | + | bool operator!=( const std::{{#var:cont}}<{{#var:types_short}}>& lhs, |
− | const std::{{ | + | const std::{{#var:cont}}<{{#var:types_short}}>& rhs ); |
}} | }} | ||
− | {{dcl|num=3|since={{cpp/std|{{ | + | {{dcl|num=3|since={{cpp/std|{{#var:cont}}}}|until=c++20|1= |
template< {{#var:types}} > | template< {{#var:types}} > | ||
− | bool operator<( const std::{{ | + | bool operator<( const std::{{#var:cont}}<{{#var:types_short}}>& lhs, |
− | const std::{{ | + | const std::{{#var:cont}}<{{#var:types_short}}>& rhs ); |
}} | }} | ||
− | {{dcl|num=4|since={{cpp/std|{{ | + | {{dcl|num=4|since={{cpp/std|{{#var:cont}}}}|until=c++20|1= |
template< {{#var:types}} > | template< {{#var:types}} > | ||
− | bool operator<=( const std::{{ | + | bool operator<=( const std::{{#var:cont}}<{{#var:types_short}}>& lhs, |
− | const std::{{ | + | const std::{{#var:cont}}<{{#var:types_short}}>& rhs ); |
}} | }} | ||
− | {{dcl|num=5|since={{cpp/std|{{ | + | {{dcl|num=5|since={{cpp/std|{{#var:cont}}}}|until=c++20|1= |
template< {{#var:types}} > | template< {{#var:types}} > | ||
− | bool operator>( const std::{{ | + | bool operator>( const std::{{#var:cont}}<{{#var:types_short}}>& lhs, |
− | const std::{{ | + | const std::{{#var:cont}}<{{#var:types_short}}>& rhs ); |
}} | }} | ||
− | {{dcl|num=6|since={{cpp/std|{{ | + | {{dcl|num=6|since={{cpp/std|{{#var:cont}}}}|until=c++20|1= |
template< {{#var:types}} > | template< {{#var:types}} > | ||
− | bool operator>=( const std::{{ | + | bool operator>=( const std::{{#var:cont}}<{{#var:types_short}}>& lhs, |
− | const std::{{ | + | const std::{{#var:cont}}<{{#var:types_short}}>& rhs ); |
}} | }} | ||
{{dcl|num=7|since=c++20|1= | {{dcl|num=7|since=c++20|1= | ||
template< {{#var:types}} > | template< {{#var:types}} > | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}} |
|array|vector= | |array|vector= | ||
constexpr synth-three-way-result<T> | constexpr synth-three-way-result<T> | ||
− | operator<=>( const std::{{ | + | operator<=>( const std::{{#var:cont}}<{{#var:types_short}}>& lhs, |
− | const std::{{ | + | const std::{{#var:cont}}<{{#var:types_short}}>& rhs ); |
|#default= | |#default= | ||
synth-three-way-result<T> | synth-three-way-result<T> | ||
− | operator<=>( const std::{{ | + | operator<=>( const std::{{#var:cont}}<{{#var:types_short}}>& lhs, |
− | const std::{{ | + | const std::{{#var:cont}}<{{#var:types_short}}>& rhs );}} |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | Compares the contents of two {{tt|{{ | + | Compares the contents of two {{tt|{{#var:cont}}}}s. |
@1,2@ Checks if the contents of {{c|lhs}} and {{c|rhs}} are equal, that is, they have the same number of elements and each element in {{c|lhs}} compares equal with the element in {{c|rhs}} at the same position. | @1,2@ Checks if the contents of {{c|lhs}} and {{c|rhs}} are equal, that is, they have the same number of elements and each element in {{c|lhs}} compares equal with the element in {{c|rhs}} at the same position. | ||
− | @3-6@ Compares the contents of {{c|lhs}} and {{c|rhs}} lexicographically. The comparison is performed by a function equivalent to {{lc|std::lexicographical_compare}}. {{cpp/container/if ord|{{ | + | @3-6@ Compares the contents of {{c|lhs}} and {{c|rhs}} lexicographically. The comparison is performed by a function equivalent to {{lc|std::lexicographical_compare}}. {{cpp/container/if ord|{{#var:cont}}|This comparison ignores the {{tt|{{#var:cont}}}}'s ordering {{spar|Compare}}.}} |
− | @7@ Compares the contents of {{c|lhs}} and {{c|rhs}} lexicographically. The comparison is performed as if by calling {{box|{{c/core|std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),}}<br>{{nbspt|39}}{{c/core|rhs.begin(), rhs.end(),}}{{nbspt}}{{lti|cpp/standard library/synth-three-way}}{{c/core|)}}}}.{{cpp/container/if ord|{{ | + | @7@ Compares the contents of {{c|lhs}} and {{c|rhs}} lexicographically. The comparison is performed as if by calling {{box|{{c/core|std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),}}<br>{{nbspt|39}}{{c/core|rhs.begin(), rhs.end(),}}{{nbspt}}{{lti|cpp/standard library/synth-three-way}}{{c/core|)}}}}.{{cpp/container/if ord|{{#var:cont}}| This comparison ignores the {{tt|{{#var:cont}}}}'s ordering {{spar|Compare}}.}} |
@@ The return type is the return type of {{lti|cpp/standard library/synth-three-way}} (i.e., {{lti|cpp/standard library/synth-three-way|synth-three-way-result}}{{sep}}{{c/core|<T>}}). | @@ The return type is the return type of {{lti|cpp/standard library/synth-three-way}} (i.e., {{lti|cpp/standard library/synth-three-way|synth-three-way-result}}{{sep}}{{c/core|<T>}}). | ||
@@ If none of the following conditions is satisfied, the behavior is undefined: | @@ If none of the following conditions is satisfied, the behavior is undefined: | ||
Line 79: | Line 76: | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par|lhs, rhs|{{tt|{{ | + | {{par|lhs, rhs|{{tt|{{#var:cont}}}}s whose contents to compare}} {{#switch:{{#var:cont}} |
|array|vector|deque|list|forward_list= | |array|vector|deque|list|forward_list= | ||
{{par req named|T|EqualityComparable|overloads=1,2}} | {{par req named|T|EqualityComparable|overloads=1,2}} | ||
Line 88: | Line 85: | ||
{{par req named|T, Key|EqualityComparable|overloads=1,2}} | {{par req named|T, Key|EqualityComparable|overloads=1,2}} | ||
{{par req named|Key|LessThanComparable|overloads=3-6|notes=The ordering relation must establish total order.}} | {{par req named|Key|LessThanComparable|overloads=3-6|notes=The ordering relation must establish total order.}} | ||
− | |{{included}} | + | |{{included}} |
}} | }} | ||
− | {{par end}} | + | {{par end}} |
===Return value=== | ===Return value=== | ||
− | @1@ {{c|true}} if the contents of the {{tt|{{ | + | @1@ {{c|true}} if the contents of the {{tt|{{#var:cont}}}}s are equal, {{c|false}} otherwise. |
− | @2@ {{c|true}} if the contents of the {{tt|{{ | + | @2@ {{c|true}} if the contents of the {{tt|{{#var:cont}}}}s are not equal, {{c|false}} otherwise. |
@3@ {{c|true}} if the contents of the {{c|lhs}} are lexicographically ''less'' than the contents of {{c|rhs}}, {{c|false}} otherwise. | @3@ {{c|true}} if the contents of the {{c|lhs}} are lexicographically ''less'' than the contents of {{c|rhs}}, {{c|false}} otherwise. | ||
Line 108: | Line 105: | ||
===Complexity=== | ===Complexity=== | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}}|array|forward_list=Linear in the size of the {{tt|{{#var:cont}}}}. |
|#default= | |#default= | ||
− | @1,2@ Constant if {{c|lhs}} and {{c|rhs}} are of different size, otherwise linear in the size of the {{tt|{{ | + | @1,2@ Constant if {{c|lhs}} and {{c|rhs}} are of different size, otherwise linear in the size of the {{tt|{{#var:cont}}}}. |
− | @3-7@ Linear in the size of the {{tt|{{{1}}}}}. | + | @3-7@ Linear in the size of the {{tt|{{#var:cont}}}}. |
+ | }} | ||
+ | |||
+ | ===Notes=== | ||
+ | {{rrev multi|rev1= | ||
+ | The relational operators are defined in terms of the element type's {{c/core|operator<}}. | ||
+ | |since2=c++20|rev2= | ||
+ | The relational operators are defined in terms of {{lti|cpp/standard library/synth-three-way}}, which uses {{c/core|1=operator<=>}} if possible, or {{c/core|operator<}} otherwise. | ||
+ | |||
+ | Notably, if the element does not itself provide {{c/core|1=operator<=>}}, but is implicitly convertible to a three-way comparable type, that conversion will be used instead of {{c/core|operator<}}. | ||
}} | }} | ||
===Example=== | ===Example=== | ||
<!--temp set container to test code below--> | <!--temp set container to test code below--> | ||
− | {{#vardefine:container|{{ | + | {{#vardefine:container|{{#var:cont}}}} |
{{#vardefine:headers| | {{#vardefine:headers| | ||
#include <cassert> | #include <cassert> | ||
+ | #include <compare> | ||
#include <{{Template:cpp/container/get header|{{#var:container}}}}> | #include <{{Template:cpp/container/get header|{{#var:container}}}}> | ||
}} | }} | ||
Line 185: | Line 192: | ||
std::{{#var:container}}<int, char> b{<!---->{1, 'a'}, {2, 'b'}, {3, 'c'}<!---->}; | std::{{#var:container}}<int, char> b{<!---->{1, 'a'}, {2, 'b'}, {3, 'c'}<!---->}; | ||
std::{{#var:container}}<int, char> c{<!---->{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}<!---->}; | std::{{#var:container}}<int, char> c{<!---->{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}<!---->}; | ||
− | + | ||
{{#var:common_body}} | {{#var:common_body}} | ||
} | } | ||
Line 193: | Line 200: | ||
===Defect reports=== | ===Defect reports=== | ||
{{dr list begin}} | {{dr list begin}} | ||
− | {{#ifeq:{{ | + | {{#ifeq:{{#var:cont}} |
|vector= | |vector= | ||
{{dr list item|wg=lwg|dr=469|std=C++98|before={{ltt|cpp/container/vector bool|std::vector<bool>}} specialized the comparison operators|after=removed the specializations}} | {{dr list item|wg=lwg|dr=469|std=C++98|before={{ltt|cpp/container/vector bool|std::vector<bool>}} specialized the comparison operators|after=removed the specializations}} |
Latest revision as of 03:05, 29 August 2024
Defined in header <vector>
|
||
template< class T, class Alloc > bool operator==( const std::vector<T, Alloc>& lhs, |
(1) | (constexpr since C++20) |
template< class T, class Alloc > bool operator!=( const std::vector<T, Alloc>& lhs, |
(2) | (until C++20) |
template< class T, class Alloc > bool operator<( const std::vector<T, Alloc>& lhs, |
(3) | (until C++20) |
template< class T, class Alloc > bool operator<=( const std::vector<T, Alloc>& lhs, |
(4) | (until C++20) |
template< class T, class Alloc > bool operator>( const std::vector<T, Alloc>& lhs, |
(5) | (until C++20) |
template< class T, class Alloc > bool operator>=( const std::vector<T, Alloc>& lhs, |
(6) | (until C++20) |
template< class T, class Alloc > constexpr synth-three-way-result<T> |
(7) | (since C++20) |
Compares the contents of two vector
s.
rhs.begin(), rhs.end(),
synth-three-way).-
T
modelsthree_way_comparable
. -
<
is defined for values of type (possibly const-qualified)T
, and<
is a total ordering relationship.
The |
(since C++20) |
Contents |
[edit] Parameters
lhs, rhs | - | vector s whose contents to compare
|
-T must meet the requirements of EqualityComparable in order to use overloads (1,2).
| ||
-T must meet the requirements of LessThanComparable in order to use overloads (3-6). The ordering relation must establish total order.
|
[edit] Return value
vector
s are equal, false otherwise.vector
s are not equal, false otherwise.[edit] Complexity
vector
.vector
.[edit] Notes
The relational operators are defined in terms of the element type's operator<. |
(until C++20) |
The relational operators are defined in terms of synth-three-way, which uses operator<=> if possible, or operator< otherwise. Notably, if the element does not itself provide operator<=>, but is implicitly convertible to a three-way comparable type, that conversion will be used instead of operator<. |
(since C++20) |
[edit] Example
#include <cassert> #include <compare> #include <vector> int main() { const std::vector a{1, 2, 3}, b{1, 2, 3}, c{7, 8, 9, 10}; assert ("" "Compare equal containers:" && (a != b) == false && (a == b) == true && (a < b) == false && (a <= b) == true && (a > b) == false && (a >= b) == true && (a <=> b) != std::weak_ordering::less && (a <=> b) != std::weak_ordering::greater && (a <=> b) == std::weak_ordering::equivalent && (a <=> b) >= 0 && (a <=> b) <= 0 && (a <=> b) == 0 && "Compare non equal containers:" && (a != c) == true && (a == c) == false && (a < c) == true && (a <= c) == true && (a > c) == false && (a >= c) == false && (a <=> c) == std::weak_ordering::less && (a <=> c) != std::weak_ordering::equivalent && (a <=> c) != std::weak_ordering::greater && (a <=> c) < 0 && (a <=> c) != 0 && (a <=> c) <= 0 && ""); }
[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 3431 | C++20 | operator<=> did not require T to model three_way_comparable
|
requires |