strict-weak-ordering

Operator< and strict weak ordering

一世执手 提交于 2019-12-27 08:23:28
问题 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. 回答1: if (a1 < b1) return true; if (b1 < a1) return false; // a1==b1: continue with element 2 if (a2 < b2) return true; if (b2 < a2) return false; // a2 == b2: continue with element 3 if (a3 < b3) return true; return false; // early out This orders the elements by a1 being

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

为君一笑 提交于 2019-12-19 09:18:26
问题 [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 still. And I also read the definition of strict weak ordering in the document: StrictWeakOrdering The first three axioms, irreflexivity, antisymmetry, and transitivity, are the definition of a partial ordering; transitivity of equivalence is required by the

Sorted set without a strict weak ordering

南楼画角 提交于 2019-12-12 01:22:15
问题 I have the following problem: Consider this (simplified) structure: struct Task { int priority; std::string description; // Some other fields }; Now I want to have a set of all tasks and do some work with it. Therefore I have an equality operator, which checks that every element is equal. bool isEqual(const Task& lhs, const Task& rhs) { return lhs.priority == rhs.priority && lhs.description == rhs.description && // Some other fields ; } For this I have used the std::unordered_set, which

Doesnt stl sort require a strict weak ordering to work?

﹥>﹥吖頭↗ 提交于 2019-12-10 17:56:05
问题 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 sort a sequence. less_equal would be used in that algorithm in the following manner: vector<int> vec1; sort(vec1.begin(), vec1.end(),less_equal<int>()); -- Now I am confused, is the documentation above correct ? 回答1: You are right, std::sort requires

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

不想你离开。 提交于 2019-12-06 02:06:20
问题 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 out-of-bound address when the compare function is not strict weak order? What is it trying to compare? Also I wonder if there are other pitfalls in STL that I should be aware of. 回答1: The first thing is that calling the algorithm with a comparator that

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

孤者浪人 提交于 2019-12-01 06:47:47
[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 still. And I also read the definition of strict weak ordering in the document: StrictWeakOrdering The first three axioms, irreflexivity, antisymmetry, and transitivity, are the definition of a partial ordering; transitivity of equivalence is required by the definition of a strict weak ordering. A total ordering is one that satisfies an even stronger condition:

stl ordering - strict weak ordering

五迷三道 提交于 2019-11-27 14:01:28
Why does STL work with a comparison function that is strict weak ordering ? Why can't it be partial ordering? 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? curiousguy 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

stl ordering - strict weak ordering

旧城冷巷雨未停 提交于 2019-11-26 16:34:31
问题 Why does STL work with a comparison function that is strict weak ordering? Why can't it be partial ordering? 回答1: 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? 回答2: Simply, a strict weak ordering is defined as an ordering that defines

Implementing comparison operators via &#39;tuple&#39; and &#39;tie&#39;, a good idea?

[亡魂溺海] 提交于 2019-11-26 01:45:02
问题 (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 downsides though are the pretty much useless variable names. Even if I myself created that typedef , I won\'t remember 2 days later what first and what second exactly was, especially if they are both of the same type. This gets even worse for