STL的sort函数

sort函数主要是用来进行特定数据类型排序的,但是呢他的使用也是有限制的,并不是想我的那般容易,他的使用的形式并不是唯一的,或者说他的使用是在特定情况下才能使用

的现在呢主要说两种,一种是比较常见的,sort排序,含义就是sort(_First,_Last,Comp);这样的一种形式,这种形式下,也就是常见的一种形式,这种形式是这样的

#include<deque>
#include<algorithm>
#include<iostream>
using namespace std;
struct stuInfo
{
	int num;
	char name[30];
};
//比较函数对象
bool stuCmpFunc(const stuInfo a,const stuInfo b)
{
	return a.num>b.num;
}
//比较结构体
struct stuCmpStr
{
	bool operator()(const stuInfo a,const stuInfo b)const
	{
		return a.num>b.num;
	}
};

int main()
{
	deque<stuInfo> stuList;
	stuInfo stu_1={30,"zjy"};
	stuInfo stu_2={31,"zjy"};
	stuInfo stu_3={341,"zjy"};
	stuInfo stu_4={34,"zjy"};
	stuInfo stu_6={4,"zjy"};
	stuList.push_back(stu_1);
	stuList.push_back(stu_2);
	stuList.push_back(stu_2);
	stuList.push_back(stu_3);
	stuList.push_back(stu_4);
	stuList.push_back(stu_6);
	cout<<"使用函数对象排序\n";
	sort(stuList.begin(),stuList.end(),stuCmpFunc);	
	for(deque<stuInfo>::iterator itr=stuList.begin();itr!=stuList.end();itr++)
	{
		cout<<(*itr).num<<endl;
		cout<<(*itr).name<<endl;
	}
	stuList.clear();
	stuList.push_back(stu_1);
	stuList.push_back(stu_2);
	stuList.push_back(stu_2);
	stuList.push_back(stu_3);
	stuList.push_back(stu_4);
	stuList.push_back(stu_6);
	sort(stuList.begin(),stuList.end(),stuCmpStr());
	cout<<"使用结构体对象排序\n";
	sort(stuList.begin(),stuList.end(),stuCmpFunc);	
	for(deque<stuInfo>::iterator itr=stuList.begin();itr!=stuList.end();itr++)
	{
		cout<<(*itr).num<<endl;
		cout<<(*itr).name<<endl;
	}
	return 0;
}

但是呢,这样并不算玩,因为呢,还有一种情况下使用sort排序,总是会出错,就是list容器,当你使用这种容器的时候,sort(_First,_Last,Comp)总是不能成功,或许是我的代码有问题吧,但是呢,使用list自身的sort函数模板也是可以成功的,如下所示,即可

#include "stdafx.h"

#include<list>
#include<algorithm>
#include<iostream>
using namespace std;
struct stuInfo
{
	int num;
	char name[30];
};


class cus_find
{
private :
	int num;
public:
	cus_find(int a):num(a)
	{
		;
	}
	bool operator()(const stuInfo a)const
	{
		if(a.num==num)
			return true;
	}
};
struct stuSort
{
	bool operator()(const stuInfo a,const stuInfo b)const
	{
		return a.num>b.num;
	}
};
bool cmp( const stuInfo& a,const stuInfo &b) 
{
	return a.num<b.num;
}
int _tmain(int argc, _TCHAR* argv[])
{

	list<stuInfo> stuList;
	stuInfo stu_1={30,"zjy"};
	stuInfo stu_2={31,"zjy"};
	stuInfo stu_3={341,"zjy"};
	stuInfo stu_4={34,"zjy"};
	stuInfo stu_6={4,"zjy"};
	stuList.push_back(stu_1);
	stuList.push_back(stu_2);
	stuList.push_back(stu_2);
	stuList.push_back(stu_3);
	stuList.push_back(stu_4);
	stuList.push_back(stu_6);	
	<strong><span style="font-size:24px;">stuList.sort(stuSort());</span></strong>
	for(list<stuInfo>::iterator itr=stuList.begin();itr!=stuList.end();itr++)
	{
		cout<<(*itr).num<<endl;
	}
	list<stuInfo>::iterator itr;
	itr=find_if(stuList.begin(),stuList.end(),cus_find(30));
	if(itr!=stuList.end())
	{
		cout<<"we find the student\n";
		cout<<(*itr).name<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世纪殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值