在C++中,用STL的list::sort()排序自定义的数据?

hua5425 2008-03-21 06:47:49
#include <list>
#include <iostream>
#include <algorithm>
using namespace std;
/*拷贝到C++工程中可以啦,谢谢*/
class Stu
{
public:
Stu(){strcpy(name,"hua");num=0;}
Stu(char sname[20],int snum){strcpy(name,sname);num=snum;}
~Stu(){}
char* GetName()
{
return name;
}
int GetNum()
{
return num;
}
protected:
private:
char name[20];
int num;
};

int main(void){


/* list<Stu> stu;
stu.push_front(Stu("hua",0));
stu.push_front(Stu("xue",1));
stu.push_front(Stu("xiang",2));

list<Stu>::iterator i,iend;
iend = stu.end();
for (i = stu.begin(); i != iend; i++)
{
cout<<i->GetName()<<" ";
}
stu.sort();*/ //不可用
list<int> sl;
sl.push_front(11);
sl.push_front(23);
sl.push_front(39);
//打印单向链表元素
list<int>::iterator i,iend;
iend=sl.end();
for(i=sl.begin(); i!=iend; i++)
cout << *i << ' ';
cout << endl;
sl.sort();//可以用
for(i=sl.begin(); i!=iend; i++)
cout << *i << ' ';
cout << endl;
return 0;
}
...全文
604 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
hua5425 2008-03-24
  • 打赏
  • 举报
回复
哈哈,自己搞定了啊,好开心啊[color=#FF0000][/color]
hua5425 2008-03-24
  • 打赏
  • 举报
回复
还是不行啊
mr.zhoux 2008-03-22
  • 打赏
  • 举报
回复
帮你改了

#include <list>
#include <iostream>
#include <algorithm>
using namespace std;
/*拷贝到C++工程中可以啦,谢谢*/
class Stu
{
public:
Stu(){strcpy(name,"hua");num=0;}
Stu(char sname[20],int snum){strcpy(name,sname);num=snum;}
~Stu(){}
char* GetName()
{
return name;
}
int GetNum()
{
return num;
}

bool operator >(const Stu A)
{
return num > A.num;
}

bool operator <(const Stu A)
{
return num < A.num;
}

bool operator = (const Stu A)
{
return num == A.num;
}
int num;
protected:
private:
char name[20];
};

int main(void)
{
list <Stu> stu;
stu.push_front(Stu("hua",0));
stu.push_front(Stu("xue",1));
stu.push_front(Stu("xiang",2));

list <Stu>::iterator i,iend;
iend = stu.end();
for (i = stu.begin(); i != iend; i++)
{
cout <<i->GetName() <<" ";
}
stu.sort(); //不可用
cout<<endl;
for (i = stu.begin(); i != iend; i++)
{
cout <<i->GetName() <<" ";
}
return 0;
}

  • 打赏
  • 举报
回复
[Quote=引用 4 楼 akirya 的回复:]
增加bool operator <(const Stu& )const 这个成员函数
[/Quote]
这样写
ZiSheng 2008-03-22
  • 打赏
  • 举报
回复

bool operator<(const stu &)
{
if(strcmp(name,stu.name)<0)
return true;
else
return false;

}

楼主你试试
hua5425 2008-03-22
  • 打赏
  • 举报
回复
我的意思是我可以根据姓名name排序,也可以根据学号num,要求排序后链表是重组好的有序表,怎么重载< > = 运算符呢?
hua5425 2008-03-22
  • 打赏
  • 举报
回复
bool operator() <(const stu & lhs, const stu & rhs) const
有错
yebeans 2008-03-22
  • 打赏
  • 举报
回复
return lhs.maxSpeed() < rhs.maxSpeed(); ==》请忽视之。。。。
yebeans 2008-03-22
  • 打赏
  • 举报
回复
<是个二元操作符
试试这样

bool operator()<(const stu & lhs, const stu & rhs) const
{
return lhs.maxSpeed() < rhs.maxSpeed();
}
hua5425 2008-03-22
  • 打赏
  • 举报
回复
d:\stlport\stlport\stl\_function_base.h(67) : error C2678: binary '<' : no operator defined which takes a left-hand operand of type 'const class Stu' (or there is no acceptable conversion)
d:\stlport\stlport\stl\_function_base.h(67) : while compiling class-template member function 'bool __thiscall stlp_std::less<class Stu>::operator ()(const class Stu &,const class Stu &) const'
Error executing cl.exe.
不可以,编译原话.
systemthink 2008-03-21
  • 打赏
  • 举报
回复
stu.push_front(Stu("hua",0));
stu.push_front(Stu("xue",1));
stu.push_front(Stu("xiang",2));
================================
这种排序,可以考虑std::pair<string, int>
  • 打赏
  • 举报
回复
增加bool operator<(const Stu& )这个成员函数
yebeans 2008-03-21
  • 打赏
  • 举报
回复
你要定义Stu的比较运算符(大于,小于,等于),否则sort不知道如何比较两个Stu 对象谁大谁小
只需要重载Stu 的运算符 > < = 应该就可以了,编译的时候应该会有提示需要哪些运算符。
hua5425 2008-03-21
  • 打赏
  • 举报
回复
当然可以啦啊
mr.zhoux 2008-03-21
  • 打赏
  • 举报
回复
字符串能排序么?敢问lz ....

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧