#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;
bool cmp (const int a, const int b)
{
return fabs(a) > fabs(b);
}
int main()
{
int a[6]={1,9,2,10,7,3};
sort(a,a+6,cmp);
for(int i=0;i<5;i++)
cout<<a[i]<<' ';
cout<<endl;
return 0;
}
#include<math.h>
#include <algorithm>
using namespace std;
bool cmp (const int a, const int b)
{
return fabs(a) > fabs(b);
}
int main()
{
int a[6]={1,9,2,10,7,3};
sort(a,a+6,cmp);
for(int i=0;i<5;i++)
cout<<a[i]<<' ';
cout<<endl;
return 0;
}
本文展示了一个使用C++实现的排序示例,其中包括了自定义比较函数以按绝对值从大到小排序整数数组的方法。通过这个示例,读者可以了解到如何在C++中使用标准模板库(STL)中的sort函数以及如何定义自己的比较函数。
686

被折叠的 条评论
为什么被折叠?



