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

class A{
public:
 int a,b;
 A(int t1,int t2){a=t1,b=t2;}
};


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(node());

 

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

 return 0;

}

输出结果:1 2 4

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-18
  • 2021-06-08
  • 2022-12-23
  • 2021-06-20
相关资源
相似解决方案