sort(b, e)
sort(b, e, p)
stable_sort(b, e)
stable_sort(b, e, p)
注意:
不适用于list容器,list有成员函数sort()
STL算法- 排序算法
sort() make_heap()
stable_sort() push_heap()
partial_sort() pop_heap()
partial_sort_cop() sort_heap()
nth_element()
stable_patition()
#include<iostream>
#include<algorithm>
#include<deque>
#include<functional>
using namespace std;
int main()
{
deque<int> ideq;
for(int i = 1; i <= 9; i++)
ideq.push_back(i);
for(int i = 1; i <= 9; i++)
ideq.push_back(i);
for(deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++)
cout<<*iter<<' ';
cout<<endl;
sort(ideq.begin(), ideq.end());
for(deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++)
cout<<*iter<<' ';
cout<<endl;
sort(ideq.begin(), ideq.end(), greater<int>());
for(deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); iter++)
cout<<*iter<<' ';
cout<<endl;
//
system("pause");
return 0;
}
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
bool lessLength(const string &s1, const string &s2)
{
return s1.length() < s2.length();
}
int main()
{
vector<string> svec;
vector<string> svec2;
svec.push_back("1xxx");
svec.push_back("2x");
svec.push_back("3x");
svec.push_back("5xxx");
svec.push_back("6xxxxxx");
svec.push_back("7xxxxxx");
svec.push_back("8xxxxxx");
svec.push_back("9xxxxxx");
svec.push_back("10xxxxxx");
svec.push_back("11");
svec.push_back("12");
svec.push_back("13");
svec.push_back("14");
svec.push_back("15");
svec.push_back("16");
svec.push_back("17");
svec2 = svec;
for(vector<string>::iterator iter = svec.begin(); iter != svec.end(); iter++)
cout<< *iter <<' ';
cout<<endl;
sort(svec.begin(), svec.end(), lessLength);
for(vector<string>::iterator iter = svec.begin(); iter != svec.end(); iter++)
cout<< *iter <<' ';
cout<<endl;
stable_sort(svec2.begin(), svec2.end(), lessLength);
for(vector<string>::iterator iter = svec2.begin(); iter != svec2.end(); iter++)
cout<< *iter <<' ';
cout<<endl;
//
system("pause");
return 0;
}