Algorithm头文件中的sort函数使用

来源:互联网 发布:神奇网络热血霸业下载 编辑:程序博客网 时间:2024/05/22 19:29
#include "stdafx.h"#include<iostream>#include<algorithm>#include<string>#if 0int _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 1bool 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