我是靠谱客的博主 发嗲大碗,这篇文章主要介绍使用sort函数进行数组排序,现在分享给大家,希望可以做个参考。

实例说明:
对包含10个元素125,-26,53,12,-6,95,46,85,-45,785的数组分别进行升序和降序排列。
实现过程:

#include <iostream>
#include <algorithm>
using namespace std;
bool complare(int a,int b)
{
return a>b;
}
int main()
{
int a[10]= {125,-26,53,12,-6,95,46,85,-45,785};
sort(a,a+10);
//从小到大排序
for(int i=0; i<10; i++)
cout<<a[i]<<",";
cout<<endl;
sort(a,a+10,complare);//在这里就不需要对complare函数传入参数了,//这是规则
for(int i=0; i<10; i++)
cout<<a[i]<<",";
//从大到小排序
cout<<endl;
/*or
for(int i=9;i>=0;i--)
cout<<a[i]<<",";
cout<<endl;*/
}

最后

以上就是发嗲大碗最近收集整理的关于使用sort函数进行数组排序的全部内容,更多相关使用sort函数进行数组排序内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部