简体   繁体   English

将这些元素按降序排序?

[英]Sort these elements in descending order?

Experimenting with qsort and it runs perfectly for me. 试用qsort,它对我来说运行非常完美。 I use function pointers throughout the program and some other features I am not used to (ie such as void pointers). 我在整个程序中使用函数指针以及一些我不习惯的功能(例如,无效指针)。

I want the elements arranged in descending order (ie as opposed to ascending order), however. 但是,我希望元素以降序排列(即与升序相反)。 What can I do to achieve this? 我该怎么做?

Here is the code: 这是代码:

#include <iostream>
#include <cstdlib>  // Required for qsort
#include <cstring>
using std::cout;
using std::endl;

int compare_strs( const void *arg1, const void *arg2 );
int compare_ints( const void* arg1, const void* arg2 );

int main()
{
    char * shrooms[10] = 
    {
        "Matsutake", "Lobster", "Oyster", "King Boletus",
        "Shaggy Mane", "Morel", "Chanterelle", "Calf Brain",
        "Pig's Ear", "Chicken of the Woods"
    };

    int nums[10] = {99, 43, 23, 100, 66, 12, 0, 125, 76, 2};

    // The address of the array, number of elements
    // the size of each element, the function pointer to 
    // compare two of the elements
    qsort( (void *)shrooms, 10, sizeof( char * ), compare_strs ); 
    qsort( (void *)nums, 10, sizeof( int * ), compare_ints ); 

    // Output sorted lists
    for ( int i = 0; i < 10; ++i )
        cout << shrooms[i] << endl;

    for ( int i = 0; i < 10; ++i )
        cout << nums[i] << endl;

    return 0;
}

int compare_ints( const void * arg1, const void * arg2 )
{
    int return_value = 0;

    if ( *(int *)arg1 < *(int *)arg2 )
        return_value = -1;
    else if ( *(int *)arg1 > *(int *)arg2 )
        return_value = 1;

    return return_value;
}

int compare_strs( const void * arg1, const void * arg2 )
{
    return ( _stricmp( *(char **) arg1, *(char **) arg2 ) );
}

The program outputs in ascending order (ie starting with Calf Brain), but I am trying to get it to start with Shaggy Mane (ie descending order). 该程序以升序输出(即从小腿大脑开始),但我试图让它从毛茸茸的鬃毛(即降序)开始。 Any help would be much appreciated. 任何帮助将非常感激。

Use std::sort in conjunction with std::string and std::greater : std::sortstd::stringstd::greater结合使用:

std::string shrooms[10] = 
{
    "Matsutake", "Lobster", "Oyster", "King Boletus",
    "Shaggy Mane", "Morel", "Chanterelle", "Calf Brain",
    "Pig's Ear", "Chicken of the Woods"
};

std::sort(shrooms, shrooms+10, std::greater<std::string>);

If you don't want to use std::sort simply inverse either the result of your comparison function or reverse your result. 如果您不想使用std::sort只需将比较函数的结果取反或取反即可。

Better use std::sort . 最好使用std :: sort There is no need to play around complicated qsort . 无需绕过复杂的qsort Also, you should use std::string for storing strings, and std::vector to store them! 另外,您应该使用std::string来存储字符串,并使用std::vector来存储它们!

EDIT: Someone posted a commenet that std::sort won't magically reverse the sorting logic, so here is my reply: 编辑: 有人发布了一个std :: sort的commenet不会神奇地逆转排序逻辑,所以这是我的回复:

And why not? 那么为何不? std::sort algorithm takes a comparator also! std::sort算法也需要一个比较器! Return negative-Boolean value, and you are done! 返回负布尔值,您就完成了!

Reverse the logic of your comparator functions. 反转比较器功能的逻辑。

inline int rcompare_strs( const void *arg1, const void *arg2 )
{
    return -1*compare_strs(arg1, arg2);
}

inline int rcompare_ints( const void* arg1, const void* arg2 )
{
    return -1*compare_ints(arg1, arg2);
}

qsort( (void *)shrooms, 10, sizeof( shrooms[0] ), rcompare_strs ); 
qsort( (void *)nums, 10, sizeof( nums[0] ), rcompare_ints ); 

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

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