如何在 ASC 和 DESC 模式下对 C++ 数组进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4008253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:15:57  来源:igfitidea点击:

How to sort C++ array in ASC and DESC mode?

c++arrayssorting

提问by cpp_best

I have this array:

我有这个数组:

array[0] = 18;
array[1] = -10;
array[2] = 2;
array[3] = 4;
array[4] = 6;
array[5] = -12;
array[6] = -8;
array[7] = -6;
array[8] = 4;
array[9] = 13;

how do I sort the array in asc/desc mode in C++?

如何在 C++ 中以 asc/desc 模式对数组进行排序?

回答by Armen Tsirunyan

To sort an array in ascending, use:

要按升序对数组进行排序,请使用:

#include <algorithm>

int main()
{
   //...
   std::sort(array, array+n); //where n is the number of elements you want to sort
}

To sort it in descending, use

要按降序对其进行排序,请使用

#include <algorithm>
#include <functional>  

int main()
{
   //...
   std::sort(array, array+n, std::greater<int>());
}

HTH

HTH

回答by Kirill V. Lyadvinsky

You can pass custom comparison functor to the std::sortfunction.

您可以将自定义比较函子传递给std::sort函数。

回答by Erik Noren

Well first I'm hoping your array assignment was just an error when posting but all your numbers are being assigned to the same memory location. There's nothing to sort.

首先,我希望您的数组分配在发布时只是一个错误,但是您的所有数字都被分配到了相同的内存位置。没什么好整理的。

After that, you can use the sort()function. The example linked shows an easy method for using it. Note that there is a third parameter that's not being used that will specify how to compare the elements. By default if you don't specify the parameter it uses 'less-than' so you get an ascending order sort. Change this to specify 'greater-than' comparator to get a descending order sort.

之后,您可以使用sort()函数。链接的示例显示了使用它的简单方法。请注意,还有第三个未使用的参数将指定如何比较元素。默认情况下,如果您不指定参数,它将使用“小于”,因此您将获得升序排序。更改此项以指定“大于”比较器以获得降序排序。

回答by love

#include <iostream>
#include <stdlib.h>
using namespace std;

int main (int argc, char *argv[])
    {
        int num[10]={18,-10,2,4,6,-12,-8,-6,13,-1};
        int temp;

        cout << "Ascending Sort : \n\n";

        for(int i=0; i<=10; i++)
            {
                for(int j=i+1; j<=10; j++)
                    {
                        if(num[i]>num[j])
                            {
                                temp=num[i];
                                num[i]=num[j];
                                num[j]=temp;
                            }
                    }
                cout << num[i] << "\n";
            }

        cout << "\nDescending Sort : \n\n";

        for(int i=0; i<=10; i++)
            {
                for(int j=i+1; j<=10; j++)
                    {
                        if(num[i]<num[j])
                            {
                                temp=num[j];
                                num[j]=num[i];
                                num[i]=temp;
                            }
                    }
                cout << num[i] << "\n";
            }

    return 0;
}

回答by nonopolarity

Generally, you can just swap the two variables in

通常,您可以交换两个变量

http://www.cplusplus.com/reference/algorithm/sort/

http://www.cplusplus.com/reference/algorithm/sort/

Change

改变

bool myfunction (int i,int j) { return (i<j); }

to

bool myfunction (int i,int j) { return (j<i); }

you can rename it to something else so that you have two comparison functions to use when the result needs to be ascending or descending.

您可以将其重命名为其他名称,以便在结果需要升序或降序时使用两个比较函数。

If the function body has complicated expressions and involves iand jmultiple times, then it is easier to swap the iand jin the parameter list instead of every iand jin the body:

如果函数体有复杂的表达式并且涉及ij多次,那么在参数列表中交换ij而不是在体中的每个i和更容易j

bool myfunction (int j,int i) { return (i<j); }

The same goes for

同样适用于

http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/

http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/