Why does STL work with a comparison function that is strict weak ordering? Why can't it be partial ordering?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- What are the problems associated to Best First Sea
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
Quoting the answer given here:
A partial order would not be sufficient to implement some algorithms, such as a sorting algorithm. Since a partially ordered set does not necessarily define a relationship between all elements of the set, how would you sort a list of two items that do not have an order relationship within the partial order?
You cannot perform binary search with partial ordering. You cannot create a binary search tree with partial ordering. What functions/datatypes from algorithm need ordering and can work with partial ordering?
Simply, a strict weak ordering is defined as an ordering that defines a (computable) equivalence relation. The equivalence classes are ordered by the strict weak ordering: a strict weak ordering is a strict ordering on equivalence classes.
A partial ordering (that is not a strict weak ordering) does not define an equivalence relation, so any specification using the concept of "equivalent elements" is meaningless with a partial ordering that is not a strict weak ordering. All STL associative containers use this concept at some point, so all these specifications are meaningless with a partial ordering that is not a strict weak ordering.
Because a partial ordering (that is not a strict weak ordering) does not necessarily define any strict ordering, you cannot "sort elements" in the common sense according to partial ordering (all you can do is a "topological sort" which has weaker properties).
Given
S
<
overS
x
inS
you can define a partition of
S
(every element ofS
is either inL(x)
,I(x)
orG(x)
):A sequence is sorted according to
<
iff for everyx
in the sequence, elements ofL(x)
appear first in the sequence, followed by elements ofI(x)
, followed by elements ofG(x)
.A sequence is topologically sorted iff for every element
y
that appears after another elementx
in the sequence,y
is not less thanx
. It is a weaker constraint than being sorted.It is trivial to prove that every element of
L(x)
is less than any element ofG(x)
. There is no general relation between elements ofL(x)
and elements ofI(x)
, or between elements ofI(x)
and elements ofG(x)
. However, if<
is a strict weak ordering, than every element ofL(x)
is less than any element ofI(x)
, and than any element ofI(x)
is less than any element ofG(x)
.If
<
is a strict weak ordering, andx<y
then any element ofL(x) U I(x)
is less then any elementI(y) U G(y)
: any element not greater thanx
is less than any element not less thaty
. This does not necessarily hold for a partial ordering.