简体   繁体   English

这是文档中的小错误,还是我缺少什么?

[英]Is this a minor error in the documentation or am I missing something?

I know this is a minor thing but I want to be precise on my understanding of std::sort() . 我知道这是一件小事,但我想对我的std::sort()有所了解。

Given the function template 给定功能模板

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

one can read the following here (my emphasis): 可以在这里阅读以下内容(我的重点):

comp - comparison function object (ie an object that satisfies the requirements of Compare) which returns ​true if the first argument is less than the second. comp-比较函数对象(即满足Compare要求的对象),如果第一个参数小于第二个参数,则返回true。

But in the example right below one can see that neither the std::greater<int>() nor the lambda-expression [](int a, int b) { return b < a; } 但是在下面的示例中,您不会看到std::greater<int>()或lambda-expression [](int a, int b) { return b < a; } [](int a, int b) { return b < a; } satisfy this requirement for the Compare function object. [](int a, int b) { return b < a; }满足Compare函数对象的这一要求。

That is correct if you read that along with this ( taken from the same doc ): 如果您同时阅读以下内容( 来自同一文档 ),那是正确的:

Sorts the elements in the range [first, last) in ascending order . 按升序对[first,last)范围内的元素进行排序。

That is, to sort the range in increasing order, the cmp must return true if first argument is less than second, else the range will be sorted in decreasing order. 也就是说,要以递增顺序对范围进行排序,如果第一个参数小于第二个参数,则cmp必须返回true ,否则范围将以递减顺序进行排序。 Both the examples which use std::greater<int> and lambda are used to sort the range in decreasing order! 使用std::greater<int>和lambda的两个示例均用于按降序对范围进行排序!

It is somewhat confusing at first read but if you look a the Type requirements section it says: 初读时有些令人困惑,但是如果您看到“ Type requirements部分,则会显示:

Compare must meet the requirements of Compare. 比较必须满足比较的要求。

which points to C++ concepts: Compare which says ( emphasis mine ): 哪个指向C ++概念:比较哪个说( 重点是 ):

The return value of the function call operation applied to an object of type Compare, when contextually 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 Compare type , and false otherwise. 应用于上下文类型为Object的对象的函数调用操作的返回值在上下文中转换为bool时, 如果在此Compare类型引起的严格弱排序关系中,调用的第一个参数出现在第二个参数之前 ,则返回true,否则返回false 。

and goes on to establish the requirements for comp(a, b) which is that it establishes strict weak ordering relation with the following properties: 并继续建立comp(a, b)的要求,即建立具有以下特性的严格的弱序关系:

  • For all a, comp(a,a)==false 对于所有a,comp(a,a)== false
  • If comp(a,b)==true then comp(b,a)==false 如果comp(a,b)== true,则comp(b,a)== false
  • if comp(a,b)==true and comp(b,c)==true then comp(a,c)==true 如果comp(a,b)== true和comp(b,c)== true,则comp(a,c)== true

It's supposed to compare less in terms of sorting objects from smaller to greater, ie ascending order , according to the definition of the sorting comparator. 它应该在从较小的更大,即递增顺序排序对象来比较 ,根据排序比较的定义。

The interface between std::sort and the comparator is defined it terms of "compare two objects and tell me which one appear earlier in the output". std :: sort和比较器之间的接口定义为“比较两个对象,并告诉我哪个对象出现在输出中的更早位置”。 Less is just what the std::sort docs define as "earlier in the output", not less than. 就是什么的std ::排序文档定义为“早在输出”,不小于。

For greater , "compares-greater-than" are to appear earlier in the output, so it tells the sorter that these are of "less" value, so that the sorting algorithm gets right according to what we tell them. 因为greater ,“ compares-greater-than”将在输出中更早出现,所以它告诉分选器这些值“较小”,从而使分选算法根据我们告诉他们的内容正确。

It's all about separating the algorithm (sorting according to some order) and the actual comparison. 这都是关于分离算法(按某种顺序排序)和实际比较的。 These two need an interface. 这两个需要一个接口。

If you think in terms of Rhubarb Pies (my favourite "abstract" object), they can be compared for radius (then we need to return true if Pie A if it's radius is less than Pie B). 如果您以大黄派(我最喜欢的“抽象”对象)的角度考虑,可以将它们的半径进行比较(然后,如果半径小于Pie B,则如果Pie A的半径我们需要返回true)。 We might also want to sort from tastiest to healthiest, thus returning true if Pie A has more calories than Pie B. 我们可能还希望从最美味到最健康的排序,因此如果派A的热量比派B多,则返回true。

Less means ordered below/earlier in the ascending order. 较少意味着按升序排列在下面/之前。

(Having a fourth arg bool reversed to std::sort is just unnecessary, since we can supply a lambda with an inversed operator/comparison.) (不必将第四个arg bool reversed为std :: sort,因为我们可以为lambda提供反向的运算符/比较。)

Less than something is relative to what you wants. 少于某事与您想要的有关。 What is really needed, is in fact a strict ordering. 实际上,实际上需要严格的订购。

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

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