简体   繁体   English

c++ std::vector std::sort 无限循环

[英]c++ std::vector std::sort infinite loop

I ran across an issue whenever I was trying to sort a vector of objects that was resulting in an infinite loop.每当我尝试对导致无限循环的对象向量进行排序时,我都会遇到一个问题。 I am using a custom compare function that I passed in to the sort function.我正在使用自定义比较 function,我传入排序 function。

I was able to fix the issue by returning false when two objects were equal instead of true but I don't fully understand the solution.当两个对象相等而不是 true 时,我可以通过返回 false 来解决此问题,但我并不完全理解解决方案。 I think it's because my compare function was violating this rule as outlined on cplusplus.com:我认为这是因为我的比较 function 违反了 cplusplus.com 上概述的这条规则:

Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.比较 function object 取两个与范围内相同类型的值,如果第一个参数在其定义的特定严格弱排序中位于第二个参数之前,则返回 true,否则返回 false。

Can anyone provide a more detailed explanation?谁能提供更详细的解释?

If you're looking for a detailed explanation of what 'strict weak ordering' is, here's some good reading material: Order I Say!如果您正在寻找“严格弱排序”的详细解释,这里有一些很好的阅读材料:我说的命令!

If you're looking for help fixing your comparison functor, you'll need to actually post it.如果您正在寻求修复比较函子的帮助,则需要实际发布它。

If the items are the same, one does not go before the other.如果项目相同,则一个不在另一个之前 go。 The documentation was quite clear in stating that you should return false in that case.该文档非常清楚地说明在这种情况下您应该返回false

The correct answer, as others have pointed out, is to learn what a "strict weak ordering" is.正如其他人指出的那样,正确的答案是了解什么是“严格的弱排序”。 In particular, if comp(x,y) is true, then comp(y,x) has to be false.特别是,如果comp(x,y)为真,则comp(y,x)必须为假。 (Note that this implies that comp(x,x) is false.) (请注意,这意味着comp(x,x)为假。)

That is all you need to know to correct your problem.这就是您纠正问题所需要知道的一切。 The sort algorithm makes no promises at all if your comparison function breaks the rules.如果您的比较 function 违反规则,则sort算法根本不会做出任何承诺。

If you are curious what actually went wrong, your library's sort routine probably uses quicksort internally.如果您好奇到底出了什么问题,您的库的sort例程可能在内部使用了快速排序。 Quicksort works by repeatedly finding a pair of "out of order" elements in the sequence and swapping them.快速排序通过在序列中重复查找一对“乱序”元素并交换它们来工作。 If your comparison tells the algorithm that a,b is "out of order", and it also tells the algorithm that b,a is "out of order", then the algorithm can wind up swapping them back and forth over and over forever.如果您的比较告诉算法 a,b 是“乱序的”,并且它还告诉算法 b,a 是“乱序的”,那么算法最终可以永远地来回交换它们。

The actual rule is specified in the C++ standard, in 25.3[lib.alg.sorting]/2实际规则在 C++ 标准中指定,在25.3[lib.alg.sorting]/2

Compare is used as a function object which returns true if the first argument is less than the second, and false otherwise.比较用作 function object 如果第一个参数小于第二个参数,则返回true ,否则返回false

The case when the arguments are equal falls under "otherwise". arguments 相等的情况属于“否则”。

A sorting algorithm could easily loop because you're saying that A < B AND B < A when they're equal.排序算法很容易循环,因为您说 A < B AND B < A 当它们相等时。 Thus the algorithm might infinitely try to swap elements A and B, trying to get them in the correct order.因此,该算法可能会无限尝试交换元素 A 和 B,试图让它们以正确的顺序排列。

Strict weak ordering means a < b == true and when you return true for equality its a <= b == true.严格的弱排序意味着 a < b == true 并且当您为相等返回 true 时,它的 a <= b == true。 This requirement is needed for optimality for different sort algorithms.不同排序算法的最优性需要此要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM