#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();
//
// }
sorttest
最新推荐文章于 2021-02-27 14:14:01 发布