cb49a_c++_STL_算法_对所有元素排序_sort_stable_sort
sort(b,e)
sort(b,e,p)
stable_sort(b,e)
stable_sort(b,e,p) 注意:
不适用于list容器,list有成员函数sort();
cb49a_c++_STL_算法_对所有元素排序_sort_stable_sort
sort(b,e)
sort(b,e,p)
stable_sort(b,e) //stirng,按字符个数排序.能够保证位置间前面一致。比如 22 33 55. 555, 如果按个数排序, 22能够保证一直在33前。
stable_sort(b,e,p)//
/*cb49a_c++_STL_算法_对所有元素排序_sort_stable_sort
sort(b,e)
sort(b,e,p)
stable_sort(b,e)
stable_sort(b,e,p) 注意:
不适用于list容器,list有成员函数sort();
*/ #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <deque> using namespace std; template <typename TT9>
void print9(TT9 &ideq)
{
for (TT9::iterator iter = ideq.begin(); iter != ideq.end(); ++iter)
cout << *iter << ' ';
cout << endl;
} int main()
{
deque<int> ideq; for (int i = ; i <= ; ++i)
ideq.push_back(i);
for (int i = ; i <= ; ++i)
ideq.push_back(i);
print9(ideq); sort(ideq.begin(),ideq.end());
cout << "默认是less,从小到大排序后: " << endl;
print9(ideq); //sort(ideq.begin(), ideq.end(),less<int>());//默认是less,从小到大排列
sort(ideq.begin(), ideq.end(),greater<int>());//从大到小
cout << "从大到小排序后: " << endl;
print9(ideq); return ;
}
/*cb49a_c++_STL_算法_对所有元素排序_sort_stable_sort
sort(b,e)
sort(b,e,p)
stable_sort(b,e) //stirng,按字符个数排序.能够保证位置间前面一致。比如 22 33 55. 555, 如果按个数排序, 22能够保证一直在33前。
stable_sort(b,e,p)// 注意:
不适用于list容器,list有成员函数sort();
*/ #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string> using namespace std; template <typename TT9>
void print9(TT9 &ideq)
{
for (TT9::iterator iter = ideq.begin(); iter != ideq.end(); ++iter)
cout << *iter << ' ';
cout << endl;
}
bool lessLength(const string &s1, const string &s2)
{
return s1.length() < s2.length();
} int main()
{
vector<string> svec;
vector<string> svec2;
svec.push_back("1xxxx");
svec.push_back("2x");
svec.push_back("3x");
svec.push_back("4x");
svec.push_back("5xx");
svec.push_back("6xxxx");
svec.push_back("7xx");
svec.push_back("8xxx");
svec.push_back("9xx");
svec.push_back("10xxx");
svec.push_back("");
svec.push_back("");
svec.push_back("");
svec.push_back("");
svec.push_back("14xx");
svec.push_back("");
svec.push_back(""); svec2 = svec;
print9(svec); sort(svec.begin(), svec.end(), lessLength);
print9(svec); cout << "稳定的排序:" << endl;
//stable_sort(b, e, p)//
stable_sort(svec2.begin(), svec2.end(), lessLength);
print9(svec2); return ;
}
05-26 03:03