sorttest

#include<iostream>
#include<list>
#include <algorithm>
using namespace std;

class A{
public:
	int a,b;
	A(int t1,int t2){a=t1,b=t2;}
	bool operator < (const A& t2){
		return this->a<t2.a;    
	}

};

bool lessB (const A& t1,const A& t2)
{
	return t1.b<t2.b;
}

struct node{

	bool operator()(const A& t1,const A& t2){
		return t1.a<t2.a;    //会产生升序排序,若改为>,则变为降序
	}
};





int main() {
	list<A> list_a;
	A a1(1,2), a2(4,6), a3(2,8);
	list_a.push_back(a1);
	list_a.push_back(a2);
	list_a.push_back(a3);
	list_a.sort();


	//list_a.sort(node());
	//list_a.sort(lessB());
	//sort(list_a.begin(),list_a.end(),lessB);



	list<A>::iterator ite;
	ite=list_a.begin();
	for(int i=0;i<3;i++)  {cout<<ite->a<<endl; ite++;}

	return 0;

}
// 
// 
// #include <iostream>
// #include <fstream>
// 
// #include <string>
// #include <set>
// #include <map>
// #include <vector>
// #include <list>
// #include <stack>
// #include <queue>
// 
// #include <limits>
// #include <iterator>
// #include <iomanip>
// #include <algorithm>
// #include <functional>
// 
// using namespace std;
// 
// class stu
// {
// public:
// 	int age;
// 	stu(){}
// 	stu(int a)
// 	{
// 		this->age = a;
// 	}
// 
// 
//   public:
// 	  bool operator < (stu b)
// 	  {
// 		  return this->age < b.age;
// 	  }
// 
// 	  bool operator > (stu b)
// 	  {
// 		  return this->age > b.age;
// 	  } 
// 
// 
// };
// 
// // bool less_second(const stu &a, const stu &b)
// // {
// // 	return a.age > b.age;
// // }
// 
// 
// class student
// {
// public:
// 	int age;
// 	student()
// 	{}
// 	student(int a)
// 	{
// 		this->age=a;
// 	}
// public:
// 	bool operator < (student b)
// 	{
// 		return this->age < b.age;
// 	}
// 
// 	bool operator > (student b)
// 	{
// 		return this->age > b.age;
// 	} 
// 
// 
// }; 
// 
// 
// void main()
// {
// 
// 
// 		list<student> num;
// 		num.push_back( student(1) );
// 		num.push_back( student(5));
// 		num.push_back( student(2));
// 		num.push_back( student(6));
// 		list<student>::iterator vi;
// 		greater<student> pt;
// 		num.sort(pt);
// 
// 		for( vi=num.begin();vi!=num.end();vi++)
// 		{
// 			cout  << vi->age << endl;
// 		}
// 		num.clear();
// 
// 
// 
// 
// 	//
// // 	//测试 sort();
// // 	list < stu > num;
// // 	list < stu > ::iterator Iter;
// // 	num.push_back(stu(1));
// // 	num.push_back(stu(10));
// // 	num.push_back(stu(3));
// // 	num.push_back(stu(5));
// // 	num.push_back(stu(2));
// // 
// // 	cout << "orginal:" <<endl;
// // 	for (Iter=num.begin();Iter!=num.end();++Iter)
// // 	{
// // 		cout << Iter->age <<endl;
// // 	}
// // 
// // 
// // // 	cout << "after sort(): " <<endl;
// // // 	num.sort();
// // // 	for (Iter=num.begin();Iter!=num.end();++Iter)
// // // 	{
// // // 		cout << (*Iter).age <<endl;
// // // 	}
// // 
// // 	greater<stu> stu_pt;
// // 	num.sort(stu_pt);
// // 
// // 	cout << "after sort(greater<T> _pt):"<<endl;
// // 
// // 	for (Iter=num.begin();Iter!=num.end();++Iter)
// // 	{
// // 		cout << Iter->age <<endl;
// // 	}
// // 
// // 	num.clear();
// // 
// // 	//
// // 	// part 2
// // 	//测试 sort(greater<T> _pt);
// // // 
// // // 	num.push_back( stu(1) );
// // // 	num.push_back( stu(5));
// // // 	num.push_back( stu(2));
// // // 	num.push_back( stu(6));
// // // 	list<stu>::iterator vi;
// // // 
// // // 	cout << "orginal:" <<endl;
// // // 	for( vi=num.begin();vi!=num.end();vi++)
// // // 	{
// // // 		cout  << vi->age << endl;
// // // 	}
// // // 
// // // 	// 	greater< stu > pt;
// // // 	// 	num.sort(pt);
// // // 
// // // 	cout << "after sort(greater<T> _pt):"<<endl;
// // // 	for( vi=num.begin();vi!=num.end();vi++)
// // // 	{
// // // 		cout  << vi->age << endl;
// // // 	}
// // // 
// // // // 	sort(num.begin,num.end(),less_second());
// // // // 
// // // // 	cout << "after sort(greater<T> _pt):"<<endl;
// // // // 	for( vi=num.begin();vi!=num.end();vi++)
// // // // 	{
// // // // 		cout  << vi->age << endl;
// // // // 	}
// // // 
// // // 	num.clear();
// 
// }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值