关于C++内置排序函数sort的自定义比较器cmp用法
2020年7月29日 10点22分 有同学指出这篇博客之前存在的错误,经验证,之前确实存在不少错误,现已纠正,感谢这位同学的指正,同时对之前被这篇博文误导的同学表示抱歉。
1.基础知识
C++的内置排序函数sort是一个很方便且常用的函数,默认升序排序 其头文件为#include 用法网上有很多,举个简单的例子:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector <int> array= {1,2,12,56,47,23,88,45,3,4,5,6,7};
vector<int>::iterator it;
sort(array.begin(),array.end());
for(it = array.begin(); it != array.end(); it++)
{
cout << *it << " ";
}
cout << endl;
return 0;
}
运行结果:
1 2 3 4 5 6 7 12 23 45 47 56 88
如果对int数组 a[10]