简体   繁体   English

关于将 std::less 和 std::greater 与 std::sort 一起使用的混淆

[英]confusion about using std::less and std::greater with std::sort

In C, sort usually implements as in the following example:在 C 中,sort 通常实现如下例所示:

#include <stdio.h>

void Sort( int* arr, int n, bool(*cmp)(int,int) )
{
    for( int i=0; i<n-1; i++ )
    {
        for( int j=i+1; j<n; j++ )
        {
            if( cmp(arr[i], arr[j]) )
                swap( arr[i], arr[j] );
        }
    }
}

int ascending( int a, int b ) { return a > b; }    // greater
int descending( int a, int b ) { return a < b; }   // less

void main()
{
    int arr[10] = { 1,3,5,7,9,2,4,6,8,10 };

    // ascending
    Sort( arr, 10, ascending );
    for( int i=0; i<10; i++ )
        printf( "%d ", arr[i] );

    printf( "\n" );


    // descending
    Sort( arr, 10, descending );
    for( int i=0; i<10; i++ )
        printf( "%d ", arr[i] );

    printf( "\n" );
}

So I wrote some source as in the following example, expecting same result:所以我写了一些源代码,如下例所示,期待相同的结果:

#include <iostream>
#include <algorithm>    // for sort
#include <functional>   // for less & greater
using namespace std;

bool gt( int a, int b ) { return a > b; }   // greater
bool ls( int a, int b ) { return a < b; }   // less

void main()
{
    int x[10] = { 1,3,5,7,9,2,4,6,8,10 };

    // ascending but descending
    sort( x, x+10, gt );
    for( int i=0; i<10; i++ )
        cout << x[i] << " ";

    cout << endl;

    // descending but ascending
    sort( x, x+10, ls );
    for( int i=0; i<10; i++ )
        cout << x[i] << " ";

    cout << endl;


    greater<int> g; // a > b
    less<int> l;    // a < b

    // ascending but descending
    sort( x, x+10, g );
    for( int i=0; i<10; i++ )
        cout << x[i] << " ";

    cout << endl;

    // descending but ascending
    sort( x, x+10, l );
    for( int i=0; i<10; i++ )
        cout << x[i] << " ";

    cout << endl;
}

But my expectation was not correct.但我的期望并不正确。

Why does not sort in STL work like sort in C?为什么 STL 中的排序不像 C 中的排序那样工作?

std::sort sorts in ascending order by default. std::sort默认按升序排序。 In case you are looking for descending order, here's the trick:如果您正在寻找降序,这里有诀窍:

int x[10] = { 1,3,5,7,9,2,4,6,8,10 };
std::vector<int> vec(x, x+10);          // construct std::vector object
std::sort(vec.rbegin(),vec.rend());     // sort it in reverse manner

This way, you explicitly say that std::sort should treat your array as its end is its beginning and vice versa, which results in your array being sorted in descending order.这样,您明确地说std::sort应该将您的数组视为其结束是其开始,反之亦然,这会导致您的数组按降序排序。 Here's the full example.这是完整的示例。


And in case you want to use std::less and std::greater , then it could look like this:如果您想使用std::lessstd::greater ,则它可能如下所示:

int x[10] = { 1,3,5,7,9,2,4,6,8,10 };
std::sort(x, x + 10, std::less<int>());     // for ascending order
std::sort(x, x + 10, std::greater<int>());  // for descending order

Full example with second solution is here .第二种解决方案的完整示例在这里

std::sort behaves like that because it's based on the idea of a strict weak ordering , which is (usually) defined in terms of the < operator. std::sort行为如此,因为它基于严格弱排序的思想,它(通常)根据<运算符定义。

As to your question;至于你的问题; it currently seems to be "I wrote a C function that behaves differently to std::sort . Why is it different?".目前似乎是“我编写了一个与std::sort行为不同的 C 函数。为什么不同?”。 The answer is: because you wrote a different function!答案是:因为你写了一个不同的函数!

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

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