Questions tagged [strict-weak-ordering]

37 questions
114
votes
4 answers

Implementing comparison operators via 'tuple' and 'tie', a good idea?

(Note: tuple and tie can be taken from Boost or C++11.) When writing small structs with only two elements, I sometimes tend to choose a std::pair, as all important stuff is already done for that datatype, like operator< for strict-weak-ordering. The…
Xeo
  • 129,499
  • 52
  • 291
  • 397
62
votes
7 answers

Operator< and strict weak ordering

How to define operator< on n-tuple (for example on 3-tuple) so that it satisfy strict weak ordering concept ? I know that boost library has tuple class with correctly defined operator< but for some reasons I can't use it.
Konstantin
  • 6,061
  • 8
  • 40
  • 48
22
votes
4 answers

stl ordering - strict weak ordering

Why does STL work with a comparison function that is strict weak ordering? Why can't it be partial ordering?
FL4SOF
  • 4,161
  • 6
  • 28
  • 24
15
votes
4 answers

Does a program with std::map have well-defined behaviour?

Comparing pointers to unrelated objects has unspecified results. That would seem to suggest that this program may have undefined behaviour, at the very least because we cannot guarantee a strict weak ordering on the key type: #include int…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
14
votes
4 answers

STL less operator and "invalid operator<" error

I have some code that compiles fine in VS 10.0 but after inserting a few items into the Orders map below I receive an "invalid operator <" error in Microsoft debug library. My less operator is simple, just compares the 8 byte string char by char.…
Mike Srdanovic
  • 159
  • 1
  • 3
9
votes
2 answers

What causes std::sort() to access address out of range

I understand that to use std::sort(), the compare function must be strict weak order, otherwise it will crash due to accessing address out of bound. (https://gcc.gnu.org/ml/gcc-bugs/2013-12/msg00333.html) However, why would std::sort() access…
user3512831
  • 115
  • 1
  • 4
8
votes
2 answers

Is there any implementation of Python2 where ordering is transitive?

Is there any existing implementation of Python2 where ordering is transitive? That is, where it's impossible to see this behaviour without creating user-defined types: >>> x < y < z < x True CPython is not transitive because of this…
wim
  • 338,267
  • 99
  • 616
  • 750
8
votes
1 answer

PartialOrdering, StrictWeakOrdering, TotalOrdering, what's the main difference in application

[SGI official document] Because of irreflexivity and transitivity, operator< always satisfies the definition of a partial ordering. The definition of a strict weak ordering is stricter, and the definition of a total ordering is stricter…
Joey.Z
  • 4,492
  • 4
  • 38
  • 63
7
votes
2 answers

Does greater operator ">" satisfy strict weak ordering?

Definition: Let < be a binary relation where a < b means "a is less than b". Let > be a binary relation where a > b means "a is greater than b". So, we assume < and > have meanings we usually use in a daily life. Though, in some programming…
ynn
  • 3,386
  • 2
  • 19
  • 42
6
votes
2 answers

Does using epsilon in comparison of floating-point break strict-weak-ordering?

Does following class breaks strict-weak-ordering (in comparison to regular std::less (So ignoring edge case values such as Nan)) struct LessWithEpsilon { static constexpr double epsilon = some_value; bool operator() (double lhs, double rhs)…
Jarod42
  • 203,559
  • 14
  • 181
  • 302
4
votes
2 answers

Total, weak, partial orderings - complete definition

What are the differences between strict/non-strict ordering, weak/non-weak ordering, and partial/total ordering?
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108
3
votes
2 answers

How do i sort mathematical vectors by strict weak ordering for a map?

I try to write a std::map< Vector3D, double > where colinear (parallel or anti-parallel) vectors should share the same key. As a compare function I use the following function (with a 1e-9 tolerance in isEqualEnough()) that I created with the help…
jaba
  • 735
  • 7
  • 18
3
votes
1 answer

Doesnt stl sort require a strict weak ordering to work?

From http://stdcxx.apache.org/doc/stdlibref/less-equal.html -- You can pass a less_equal object to any algorithm that requires a binary function. For example, the sort() algorithm can accept a binary function as an alternate comparison object to…
san
  • 4,144
  • 6
  • 32
  • 50
2
votes
0 answers

Does this strict weak ordering have a name (spoilers for a specific coding puzzle)

There is a coding puzzle I have encountered on one of those sites (I don't recall if it was leetcode or something else) which goes as follows: Given a list of strings, return the lexicographically smallest concatenation that uses each of the strings…
Cereal
  • 156
  • 7
2
votes
2 answers

How to force std::weak_ordering

While trying out the new Tie-Interceptor three-way comparison operator <=> I was wondering what would be an example such that struct Foo { /* .... */ auto operator<=>(const Foo &rhs) const = default; }; would lead to a compiler…
fiscblog
  • 694
  • 1
  • 12
  • 27
1
2 3