所有元素排序

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值