#include "stdafx.h"
#include<iostream>
#include<algorithm>
#include<string>
#if 0
int _tmain(int argc, _TCHAR* argv[])
{
int a[10] = {9,12,17,30,50,20,60,65,4,49};
std::sort(a, a + 10);
for (int i = 0; i < 10; i++)
std::cout << a[i] <<" ";
std::cout << std::endl;
system("pause");
return 0;
}
#endif
#if 1
bool compare(int a, int b)
{
return a < b; //默认的升序排序,如果改为return a>b,则为降序
}
bool compare_decrease(int a, int b)
{
return a > b; //从大到小的排序
}
int main()
{
int a[10] = { 9, 12, 17, 30, 50, 20, 60, 65, 4, 49 };
std::sort(a, a + 10);
for (int i = 0; i < 10; i++)
std::cout << a[i] << " ";
std::cout << std::endl;
std::sort(a, a + 10, compare_decrease);
for (int i = 0; i < 10; i++)
std::cout << a[i] << " ";
std::cout << std::endl;
system("pause");
return 0;
}
#endif
Algorithm头文件中的sort函数使用
最新推荐文章于 2024-09-21 09:04:46 发布