Difference between revisions of "cpp/named req/Compare"
m (added Spanish link) |
m (fmt.) |
||
(13 intermediate revisions by 6 users not shown) | |||
Line 4: | Line 4: | ||
{{named req|Compare}} is a set of requirements expected by some of the standard library facilities from the user-provided function object types. | {{named req|Compare}} is a set of requirements expected by some of the standard library facilities from the user-provided function object types. | ||
− | The return value of the function call operation applied to an object of a type satisfying {{named req/core|Compare}}, when [[cpp/language/ | + | The return value of the function call operation applied to an object of a type satisfying {{named req/core|Compare}}, when [[cpp/language/implicit conversion|converted]] to {{c/core|bool}}, yields {{c|true}} if the first argument of the call appears before the second in the {{enwiki|Strict weak ordering|strict weak ordering relation}} induced by this type, and {{c|false}} otherwise. |
− | As with any {{named req|BinaryPredicate}}, evaluation of that expression is not allowed to call non-const functions through the dereferenced iterators. | + | As with any {{named req|BinaryPredicate}}, evaluation of that expression is not allowed to call non-const functions through the dereferenced iterators and, syntactically, the function call operation must accept {{c/core|const}} object arguments, with the same behavior regardless of whether the arguments are {{c/core|const}} or non-{{c/core|const}}. |
===Requirements=== | ===Requirements=== | ||
Line 14: | Line 14: | ||
Given | Given | ||
− | * {{tt|comp}}, an object of type {{tt|T}} | + | * {{tt|comp}}, an object of type {{tt|T}}, |
− | * {{ | + | * {{c|equiv(a, b)}}, an [[cpp/language/expressions#Expression-equivalence|expression-equivalent]] to {{c|!comp(a, b) && !comp(b, a)}}. |
− | The following expressions must be valid and have their specified effects | + | The following expressions must be valid and have their specified effects: |
{|table class=wikitable | {|table class=wikitable | ||
Line 23: | Line 23: | ||
!Expression||Return type||Requirements | !Expression||Return type||Requirements | ||
|- | |- | ||
− | | {{c|comp(a, b)}} | + | |{{c|comp(a, b)}} |
− | | | + | |{{rrev multi|noborder=true |
− | | Establishes | + | |rev1=meets {{named req|BooleanTestable}} |
− | * For all {{tt|a}}, {{c|comp(a,a) | + | |since2=c++20|rev2=models ''{{ltt|cpp/concepts/boolean-testable}}'' |
− | * If {{c|comp(a,b) | + | }} |
− | * if {{c|comp(a,b) | + | |Establishes {{enwiki|strict weak ordering}} relation with the following properties: |
+ | * For all {{tt|a}}, {{c|1=comp(a, a) == false}}. | ||
+ | * If {{c|1=comp(a, b) == true}} then {{c|1=comp(b, a) == false}}. | ||
+ | * if {{c|1=comp(a, b) == true}} and {{c|1=comp(b, c) == true}} then {{c|1=comp(a, c) == true}}. | ||
|- | |- | ||
− | | {{c|equiv(a, b)}} | + | |{{c|equiv(a, b)}} |
− | | {{c|bool}} | + | |{{c/core|bool}} |
− | | Establishes equivalence | + | |Establishes {{enwiki|equivalence relation}}ship with the following properties: |
− | * For all {{tt|a}}, {{c|equiv(a,a) | + | * For all {{tt|a}}, {{c|1=equiv(a, a) == true}}. |
− | * If {{c|equiv(a,b) | + | * If {{c|1=equiv(a, b) == true}}, then {{c|1=equiv(b, a) == true}}. |
− | * If {{c|equiv(a,b) | + | * If {{c|1=equiv(a, b) == true}} and {{c|1=equiv(b, c) == true}}, then {{c|1=equiv(a, c) == true}}. |
|} | |} | ||
− | Note: {{tt|comp}} induces a | + | Note: {{tt|comp}} induces a {{enwiki|Total order#Strict and non-strict total orders|strict total ordering}} on the equivalence classes determined by {{tt|equiv}}. |
===Standard library=== | ===Standard library=== | ||
The following standard library facilities expect a {{named req/core|Compare}} type. | The following standard library facilities expect a {{named req/core|Compare}} type. | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/container/dsc set}} | + | {{dsc inc|cpp/container/dsc set}} |
− | {{dsc inc | cpp/container/dsc map}} | + | {{dsc inc|cpp/container/dsc map}} |
− | {{dsc inc | cpp/container/dsc multiset}} | + | {{dsc inc|cpp/container/dsc multiset}} |
− | {{dsc inc | cpp/container/dsc multimap}} | + | {{dsc inc|cpp/container/dsc multimap}} |
− | {{dsc inc | cpp/container/dsc priority_queue}} | + | {{dsc inc|cpp/container/dsc priority_queue}} |
− | {{dsc inc | cpp/algorithm/dsc sort}} | + | {{dsc inc|cpp/algorithm/dsc sort}} |
− | {{dsc inc | cpp/container/dsc sort | forward_list}} | + | {{dsc inc|cpp/container/dsc sort|forward_list}} |
− | {{dsc inc | cpp/container/dsc sort | list}} | + | {{dsc inc|cpp/container/dsc sort|list}} |
− | {{dsc inc | cpp/algorithm/dsc stable_sort}} | + | {{dsc inc|cpp/algorithm/dsc stable_sort}} |
− | {{dsc inc | cpp/algorithm/dsc partial_sort}} | + | {{dsc inc|cpp/algorithm/dsc partial_sort}} |
− | {{dsc inc | cpp/algorithm/dsc partial_sort_copy}} | + | {{dsc inc|cpp/algorithm/dsc partial_sort_copy}} |
− | {{dsc inc | cpp/algorithm/dsc is_sorted}} | + | {{dsc inc|cpp/algorithm/dsc is_sorted}} |
− | {{dsc inc | cpp/algorithm/dsc is_sorted_until}} | + | {{dsc inc|cpp/algorithm/dsc is_sorted_until}} |
− | {{dsc inc | cpp/algorithm/dsc nth_element}} | + | {{dsc inc|cpp/algorithm/dsc nth_element}} |
− | {{dsc inc | cpp/algorithm/dsc lower_bound}} | + | {{dsc inc|cpp/algorithm/dsc lower_bound}} |
− | {{dsc inc | cpp/algorithm/dsc upper_bound}} | + | {{dsc inc|cpp/algorithm/dsc upper_bound}} |
− | {{dsc inc | cpp/algorithm/dsc binary_search}} | + | {{dsc inc|cpp/algorithm/dsc binary_search}} |
− | {{dsc inc | cpp/algorithm/dsc equal_range}} | + | {{dsc inc|cpp/algorithm/dsc equal_range}} |
− | {{dsc inc | cpp/algorithm/dsc merge}} | + | {{dsc inc|cpp/algorithm/dsc merge}} |
− | {{dsc inc | cpp/container/dsc merge | forward_list}} | + | {{dsc inc|cpp/container/dsc merge|forward_list}} |
− | {{dsc inc | cpp/container/dsc merge | list}} | + | {{dsc inc|cpp/container/dsc merge|list}} |
− | {{dsc inc | cpp/algorithm/dsc inplace_merge}} | + | {{dsc inc|cpp/algorithm/dsc inplace_merge}} |
− | {{dsc inc | cpp/algorithm/dsc includes}} | + | {{dsc inc|cpp/algorithm/dsc includes}} |
− | {{dsc inc | cpp/algorithm/dsc set_difference}} | + | {{dsc inc|cpp/algorithm/dsc set_difference}} |
− | {{dsc inc | cpp/algorithm/dsc set_intersection}} | + | {{dsc inc|cpp/algorithm/dsc set_intersection}} |
− | {{dsc inc | cpp/algorithm/dsc set_symmetric_difference}} | + | {{dsc inc|cpp/algorithm/dsc set_symmetric_difference}} |
− | {{dsc inc | cpp/algorithm/dsc set_union}} | + | {{dsc inc|cpp/algorithm/dsc set_union}} |
− | {{dsc inc | cpp/algorithm/dsc push_heap}} | + | {{dsc inc|cpp/algorithm/dsc push_heap}} |
− | {{dsc inc | cpp/algorithm/dsc pop_heap}} | + | {{dsc inc|cpp/algorithm/dsc pop_heap}} |
− | {{dsc inc | cpp/algorithm/dsc make_heap}} | + | {{dsc inc|cpp/algorithm/dsc make_heap}} |
− | {{dsc inc | cpp/algorithm/dsc sort_heap}} | + | {{dsc inc|cpp/algorithm/dsc sort_heap}} |
− | {{dsc inc | cpp/algorithm/dsc is_heap}} | + | {{dsc inc|cpp/algorithm/dsc is_heap}} |
− | {{dsc inc | cpp/algorithm/dsc is_heap_until}} | + | {{dsc inc|cpp/algorithm/dsc is_heap_until}} |
− | {{dsc inc | cpp/algorithm/dsc max}} | + | {{dsc inc|cpp/algorithm/dsc max}} |
− | {{dsc inc | cpp/algorithm/dsc max_element}} | + | {{dsc inc|cpp/algorithm/dsc max_element}} |
− | {{dsc inc | cpp/algorithm/dsc min}} | + | {{dsc inc|cpp/algorithm/dsc min}} |
− | {{dsc inc | cpp/algorithm/dsc min_element}} | + | {{dsc inc|cpp/algorithm/dsc min_element}} |
− | {{dsc inc | cpp/algorithm/dsc minmax}} | + | {{dsc inc|cpp/algorithm/dsc minmax}} |
− | {{dsc inc | cpp/algorithm/dsc minmax_element}} | + | {{dsc inc|cpp/algorithm/dsc minmax_element}} |
− | {{dsc inc | cpp/algorithm/dsc lexicographical_compare}} | + | {{dsc inc|cpp/algorithm/dsc lexicographical_compare}} |
− | {{dsc inc | cpp/algorithm/dsc next_permutation}} | + | {{dsc inc|cpp/algorithm/dsc next_permutation}} |
− | {{dsc inc | cpp/algorithm/dsc prev_permutation}} | + | {{dsc inc|cpp/algorithm/dsc prev_permutation}} |
+ | {{dsc end}} | ||
+ | |||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|wg=lwg|dr=2114|paper=P2167R3|std=C++98|before=contextual convertibility of return types to {{c/core|bool}} did not<br>reflect the practice of implementations|after=requirements corrected}} | ||
+ | {{dr list item|wg=lwg|dr=3031|std=C++98|before=requirements on {{c/core|const}} values were insufficent|after=requirements strengthened}} | ||
+ | {{dr list end}} | ||
+ | |||
+ | ===See also=== | ||
+ | {{dsc begin}} | ||
+ | {{dsc inc|cpp/concepts/dsc strict_weak_order}} | ||
+ | {{dsc|[[cpp/language/operator comparison|'''Comparison operators''']]|{{tt|<}}, {{tt|1=<=}}, {{tt|1= >}}, {{tt|1= >=}}, {{tt|1= ==}}, {{tt|1= !=}}, and {{tt|1= <=>}} {{mark c++20}}, compare the arguments}} | ||
{{dsc end}} | {{dsc end}} | ||
{{langlinks|es|ja|zh}} | {{langlinks|es|ja|zh}} |
Latest revision as of 07:54, 9 September 2024
Compare is a set of requirements expected by some of the standard library facilities from the user-provided function object types.
The return value of the function call operation applied to an object of a type satisfying Compare, when converted to bool, yields true if the first argument of the call appears before the second in the strict weak ordering relation induced by this type, and false otherwise.
As with any BinaryPredicate, evaluation of that expression is not allowed to call non-const functions through the dereferenced iterators and, syntactically, the function call operation must accept const object arguments, with the same behavior regardless of whether the arguments are const or non-const.
Contents |
[edit] Requirements
The type T
satisfies Compare if
- The type
T
satisfies BinaryPredicate, and
Given
-
comp
, an object of typeT
, - equiv(a, b), an expression-equivalent to !comp(a, b) && !comp(b, a).
The following expressions must be valid and have their specified effects:
Expression | Return type | Requirements | ||||
---|---|---|---|---|---|---|
comp(a, b) |
|
Establishes strict weak ordering relation with the following properties:
| ||||
equiv(a, b) | bool | Establishes equivalence relationship with the following properties:
|
Note: comp
induces a strict total ordering on the equivalence classes determined by equiv
.
[edit] Standard library
The following standard library facilities expect a Compare type.
collection of unique keys, sorted by keys (class template) | |
collection of key-value pairs, sorted by keys, keys are unique (class template) | |
collection of keys, sorted by keys (class template) | |
collection of key-value pairs, sorted by keys (class template) | |
adapts a container to provide priority queue (class template) | |
sorts a range into ascending order (function template) | |
sorts the elements (public member function of std::forward_list<T,Allocator> )
| |
sorts the elements (public member function of std::list<T,Allocator> )
| |
sorts a range of elements while preserving order between equal elements (function template) | |
sorts the first N elements of a range (function template) | |
copies and partially sorts a range of elements (function template) | |
(C++11) |
checks whether a range is sorted into ascending order (function template) |
(C++11) |
finds the largest sorted subrange (function template) |
partially sorts the given range making sure that it is partitioned by the given element (function template) | |
returns an iterator to the first element not less than the given value (function template) | |
returns an iterator to the first element greater than a certain value (function template) | |
determines if an element exists in a partially-ordered range (function template) | |
returns range of elements matching a specific key (function template) | |
merges two sorted ranges (function template) | |
merges two sorted lists (public member function of std::forward_list<T,Allocator> )
| |
merges two sorted lists (public member function of std::list<T,Allocator> )
| |
merges two ordered ranges in-place (function template) | |
returns true if one sequence is a subsequence of another (function template) | |
computes the difference between two sets (function template) | |
computes the intersection of two sets (function template) | |
computes the symmetric difference between two sets (function template) | |
computes the union of two sets (function template) | |
adds an element to a max heap (function template) | |
removes the largest element from a max heap (function template) | |
creates a max heap out of a range of elements (function template) | |
turns a max heap into a range of elements sorted in ascending order (function template) | |
(C++11) |
checks if the given range is a max heap (function template) |
(C++11) |
finds the largest subrange that is a max heap (function template) |
returns the greater of the given values (function template) | |
returns the largest element in a range (function template) | |
returns the smaller of the given values (function template) | |
returns the smallest element in a range (function template) | |
(C++11) |
returns the smaller and larger of two elements (function template) |
(C++11) |
returns the smallest and the largest elements in a range (function template) |
returns true if one range is lexicographically less than another (function template) | |
generates the next greater lexicographic permutation of a range of elements (function template) | |
generates the next smaller lexicographic permutation of a range of elements (function template) |
[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 2114 (P2167R3) |
C++98 | contextual convertibility of return types to bool did not reflect the practice of implementations |
requirements corrected |
LWG 3031 | C++98 | requirements on const values were insufficent | requirements strengthened |
[edit] See also
(C++20) |
specifies that a relation imposes a strict weak ordering (concept) |
Comparison operators | < , <= , > , >= , == , != , and <=> (C++20), compare the arguments
|