码迷,mamicode.com
首页 > 编程语言 > 详细

STL中的自定义排序

时间:2019-11-13 16:28:39      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:lis   main   std   包括   对象   using   排序   for   col   

一 链表list的自定义排序

  如何实现链表保存学生对象,包括名字(string)和学号(int),按照学号升序排序?

//list的sort

#include<bits/stdc++.h>
using namespace std;
class stu
{
public:
    string name;
    int number;
    stu(int i,string s)
    {
        name=s;
        number=i;
    }
    void print()
    {
        cout<<number<<" "<<name<<endl;
    }
};
struct cmp
{
    bool operator()(stu & A,stu &B)
    {
        return A.number<B.number;
    }
};
int main()
{
    stu X(1,"X");
    stu Y(2,"Y");
    stu Z(3,"Z");
    list<stu>V;
    V.push_back(X);
    V.push_back(Y);
    V.push_back(Z);
    //sort(V.begin(),V.end(),cmp());
    V.sort(cmp());
    list<stu>::iterator i;
    for(i=V.begin();i!=V.end();i++)
    {
        cout<<(i->number)<<" "<<(i->name)<<endl;
        //i->print();
    }
}

 

STL中的自定义排序

标签:lis   main   std   包括   对象   using   排序   for   col   

原文地址:https://www.cnblogs.com/theda/p/11849868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!