Difference between revisions of "Template:cpp/container/operator cmp"
(Added LWG issue #469 DR.) |
m (→Example: +<compare>) |
||
(16 intermediate revisions by 4 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|1= | + | {{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|until=c++20|1= | + | {{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|until=c++20|1= | + | {{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|until=c++20|1= | + | {{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|until=c++20|1= | + | {{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|until=c++20|1= | + | {{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 | + | constexpr synth-three-way-result<T> |
− | + | operator<=>( const std::{{#var:cont}}<{{#var:types_short}}>& lhs, | |
+ | const std::{{#var:cont}}<{{#var:types_short}}>& rhs ); | ||
|#default= | |#default= | ||
− | + | synth-three-way-result<T> | |
− | + | operator<=>( const std::{{#var:cont}}<{{#var:types_short}}>& lhs, | |
+ | 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 | + | @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 {{ | + | @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>}}). | |
− | | | + | @@ If none of the following conditions is satisfied, the behavior is undefined: |
− | + | * {{tt|T}} models {{lconcept|three_way_comparable}}. | |
− | + | * {{tt|<}} is defined for values of type (possibly const-qualified) {{tt|T}}, and {{tt|<}} is a total ordering relationship. | |
− | + | ||
− | + | ||
− | {{ | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
{{rrev|since=c++20| | {{rrev|since=c++20| | ||
Line 85: | 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 94: | 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. |
− | @4@ {{c|true}} if the contents of the {{c|lhs}} are lexicographically ''less'' than or ''equal'' to the contents of {{c|rhs}}, {{c|false}} otherwise | + | @4@ {{c|true}} if the contents of the {{c|lhs}} are lexicographically ''less'' than or ''equal'' to the contents of {{c|rhs}}, {{c|false}} otherwise. |
− | @5@ {{c|true}} if the contents of the {{c|lhs}} are lexicographically ''greater'' than the contents of {{c|rhs}}, {{c|false}} otherwise | + | @5@ {{c|true}} if the contents of the {{c|lhs}} are lexicographically ''greater'' than the contents of {{c|rhs}}, {{c|false}} otherwise. |
− | @6@ {{c|true}} if the contents of the {{c|lhs}} are lexicographically ''greater'' than or ''equal'' to the contents of {{c|rhs}}, {{c|false}} otherwise | + | @6@ {{c|true}} if the contents of the {{c|lhs}} are lexicographically ''greater'' than or ''equal'' to the contents of {{c|rhs}}, {{c|false}} otherwise. |
@7@ The relative order of the first pair of non-equivalent elements in {{c|lhs}} and {{c|rhs}} if there are such elements, {{c|1=lhs.size() <=> rhs.size()}} otherwise. | @7@ The relative order of the first pair of non-equivalent elements in {{c|lhs}} and {{c|rhs}} if there are such elements, {{c|1=lhs.size() <=> rhs.size()}} otherwise. | ||
===Complexity=== | ===Complexity=== | ||
− | {{#switch:{{ | + | {{#switch:{{#var:cont}}|array|forward_list=Linear in the size of the {{tt|{{#var:cont}}}}. |
|#default= | |#default= | ||
− | @1 | + | @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 | + | <!--temp set container to test code below--> |
− | {{#vardefine: | + | {{#vardefine:container|{{#var:cont}}}} |
{{#vardefine:headers| | {{#vardefine:headers| | ||
− | #include < | + | #include <cassert> |
− | #include <{{Template:cpp/container/get header|{{#var: | + | #include <compare> |
+ | #include <{{Template:cpp/container/get header|{{#var:container}}}}> | ||
}} | }} | ||
{{#vardefine:common_body| | {{#vardefine:common_body| | ||
− | + | 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 && | ||
+ | ""); | ||
}} | }} | ||
{{example | {{example | ||
− | |||
|code= | |code= | ||
− | {{#switch: {{#var: | + | {{#switch:{{#var:container}} |
+ | |array | ||
|deque | |deque | ||
|forward_list | |forward_list | ||
Line 159: | Line 171: | ||
|set | |set | ||
|vector= | |vector= | ||
+ | {{#var:headers}} | ||
− | |||
− | |||
int main() | int main() | ||
{ | { | ||
− | std::{{#var: | + | const std::{{#var:container}} |
− | + | a{1, 2, 3}, | |
− | + | b{1, 2, 3}, | |
− | + | c{7, 8, 9{{#ifeq:{{#var:container}}|array||, 10}}<!---->}; | |
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{{#var:common_body}} | {{#var:common_body}} | ||
} | } | ||
Line 186: | Line 185: | ||
|map | |map | ||
|multimap= | |multimap= | ||
− | |||
{{#var:headers}} | {{#var:headers}} | ||
− | + | ||
int main() | int main() | ||
{ | { | ||
− | std::{{#var: | + | std::{{#var:container}}<int, char> a{<!---->{1, 'a'}, {2, 'b'}, {3, 'c'}<!---->}; |
− | std::{{#var: | + | std::{{#var:container}}<int, char> b{<!---->{1, 'a'}, {2, 'b'}, {3, 'c'}<!---->}; |
− | std::{{#var: | + | std::{{#var:container}}<int, char> c{<!---->{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}<!---->}; |
− | + | ||
{{#var:common_body}} | {{#var:common_body}} | ||
} | } | ||
− | |||
− | |||
}} | }} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}} | }} | ||
− | |||
− | |||
===Defect reports=== | ===Defect reports=== | ||
{{dr list begin}} | {{dr list begin}} | ||
− | {{dr list item|wg=lwg|dr=469|std=C++98|before={{ | + | {{#ifeq:{{#var:cont}} |
+ | |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=3431|std=C++20|before={{c/core|1=operator<=>}} did not require {{tt|T}}<br>to model {{lconcept|three_way_comparable}}|after=requires}} | ||
{{dr list end}} | {{dr list end}} | ||
− |
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 |