#include<iostream>
#include<algorithm>
#include<deque>
#include<functional>
#include<vector>
#include<string>
using namespace std;
bool lessLength(const string& s1,const string& s2)//作谓词
{
return s1.length() < s2.length();
}
void print(deque<int> &l)
{
for (auto iter = l.begin(); iter != l.end(); ++iter)
cout << *iter << " ";
cout << endl;
}
int main()
{
deque<int> ide;
for (int i = 1; i <= 9; ++i)
ide.push_back(i);
for (int i = 1; i <= 9; ++i)
ide.push_back(i);
print(ide);
//sort进行排序,从小到大
sort(ide.begin(), ide.end());
print(ide);
//从大到小
sort(ide.begin(), ide.end(), greater<int>());
print(ide);
cout << endl;
vector<string> i1;
vector<string> i2;
i1.push_back("1xxx");//按照字符串的个数进行排序
i1.push_back("2xx");
i1.push_back("3x");
i1.push_back("4");
i2 = i1;//复制一份
for (auto iter1 = i1.begin(); iter1 != i1.end(); ++iter1)
cout << *iter1 << " ";
cout << endl;
sort(i1.begin(), i1.end(), lessLength);
for (auto iter1 = i1.begin(); iter1 != i1.end(); ++iter1)
cout << *iter1 << " ";
cout << endl;
//稳定的排序,保证同等位数之间也有序排列
stable_sort(i2.begin(), i2.end(), lessLength);
for (auto iter1 = i1.begin(); iter1 != i1.end(); ++iter1)
cout << *iter1 << " ";
cout << endl;
system("pause");
return 0;
}
所有元素排序
最新推荐文章于 2023-10-22 22:24:10 发布