今天终于明白了怎么利用c++的sort函数排序了

2025-05-09 00:55:06
推荐回答(2个)
回答1:

#include"head.h"

#include 
using namespace std;
bool dy(int a,int b)
{
    return a>b;
}
bool xy(int a,int b)
{
    return a}
int main()
{
    srand(unsigned(time(NULL)));
    int a[10];
    int b[10];
    int sj=0;
    int j=0;
    int ptr=0;
    for(int i=0;i<10;++i)
    {
        a[i] = rand()%50;//1
        if(a[i]%2){
            ++sj;
            b[j++]=a[i];//2
        }
    }
    ptr=j;
    for(int i=0;i<10;++i)
    {
        if(a[i]%2==0)
        {
            b[j++]=a[i];//2
        }
    }
    for(int i=0;i<10;++i)
    {
        cout << b[i] << " ";
    }
    cout << endl;
    sort(b,b+10,xy);
    for(int i=0;i<10;++i)
    {
        cout << b[i] << " ";
    }
    return 0;
}

回答2:

使用sort()函数在做简单排序算法时候是非常好的方法