关于list的sort()的自定义排序

panchao521 2010-01-08 11:01:22
想对list用算法sort进行排序,但是排序的规则是按长度排序?
例如:
int main(int argc, char* argv[])
{
list<string> list_story;

list_story.push_back("the");
list_story.push_back("quick");
list_story.push_back("red");
list_story.push_back("fox");
list_story.push_back("jumps");
list_story.push_back("over");
list_story.push_back("the");
list_story.push_back("slow");
list_story.push_back("red");
list_story.push_back("turtle");

}
...全文
12002 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
luojian_scu 2011-07-28
  • 打赏
  • 举报
回复
我在g++下测过了,没有问题
千杯不醉-sen 2010-01-09
  • 打赏
  • 举报
回复
学习......
panchao521 2010-01-08
  • 打赏
  • 举报
回复
我用的是VC6.0,因为我现在做的项目就是用VC6.0开发的,其中想用list的sort函数定义排序规则,
楼上的几位代码,我考到我的VC6.0中编译就报错。

有人说改编译器,我想问下?如果我改了编译器的代码,那我的程序最后编译成.dll后在别的电脑上能运行吗?
ypb362148418 2010-01-08
  • 打赏
  • 举报
回复
你看看list源码就知道了,还有换个编译器
taodm 2010-01-08
  • 打赏
  • 举报
回复
珍惜生命,远离VC6
elated 2010-01-08
  • 打赏
  • 举报
回复
对list是不能使用sort算法的,list是不支持随机存取迭代器
只能用list自己的函数l.sort(op)


#include <string>
#include <list>
#include <algorithm>
#include <iostream>

using namespace std;

bool op(string str1, string str2)
{
return str1.length() < str2.length();
}

int main(int argc, char* argv[])
{
list <string> list_story;

list_story.push_back("the");
list_story.push_back("quick");
list_story.push_back("red");
list_story.push_back("fox");
list_story.push_back("jumps");
list_story.push_back("over");
list_story.push_back("the");
list_story.push_back("slow");
list_story.push_back("red");
list_story.push_back("turtle");

list_story.sort(op);

for(list<string>::iterator i = list_story.begin(); i != list_story.end(); i++)
{
cout << *i << endl;
}
}
cattycat 2010-01-08
  • 打赏
  • 举报
回复
你只要传一个你自己定义的比较函数就行,这个比较函数比较的是两个字符串,按长度返回0或1,比如11楼的
bool cmp(const string& s1,const string& s2)
{
return s1.length() < s2.length();
}
FancyMouse 2010-01-08
  • 打赏
  • 举报
回复
那改编译器
panchao521 2010-01-08
  • 打赏
  • 举报
回复
我之前读过一篇关于VC6.0下的修改list.sort的文章,
http://dev.csdn.net/article/19/19643.shtm
可我不想改sort的系统文件啊
mstlq 2010-01-08
  • 打赏
  • 举报
回复
9楼和10楼的代码都应该没问题,如果有问题,那很可能是编译器的问题(VC6?)……
kouwenlong 2010-01-08
  • 打赏
  • 举报
回复
利用7#的说法。
#include <string>
#include <list>
#include <iostream>
using namespace std;
bool cmp(const string& s1,const string& s2);
int main()
{
list <string> list_story;
list_story.push_back("the");
list_story.push_back("quick");
list_story.push_back("red");
list_story.push_back("fox");
list_story.push_back("jumps");
list_story.push_back("over");
list_story.push_back("the");
list_story.push_back("slow");
list_story.push_back("red");
list_story.push_back("turtle");
for (list<string>::iterator iter = list_story.begin();iter != list_story.end();iter++)
{
cout<<*iter<<endl;
}
cout<<"sort after"<<endl;
list_story.sort(cmp);
for (list<string>::iterator iter = list_story.begin();iter != list_story.end();iter++)
{
cout<<*iter<<endl;
}
system("pause");

}
bool cmp(const string& s1,const string& s2)
{
return s1.length() < s2.length();
}
panchao521 2010-01-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fancymouse 的回复:]
真那啥。刚发现list::sort不接受functor只接受函数指针。。。
刚才那个别看。直接写个bool cmp(const string&,const string&)然后list_story.sort(cmp);就可以了。
[/Quote]
这个我也之前试过了,可惜还是不行啊
bool cmp(const string& a,const string& b)
{
return a.size() < b.size();
}
int main(int argc, char* argv[])
{
list<string> list_story;

list_story.push_back("the");
list_story.push_back("quick");
list_story.push_back("red");
list_story.push_back("fox");
list_story.push_back("jumps");
list_story.push_back("over");
list_story.push_back("the");
list_story.push_back("slow");
list_story.push_back("red");
list_story.push_back("turtle");

list_story.sort(cmp);
return 0;
}

--------------------Configuration: LISTSORT - Win32 Debug--------------------
Compiling...
LISTSORT.cpp
E:\workspace\LISTSORT\LISTSORT.cpp(35) : error C2664: 'void __thiscall std::list<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,
class std::allocator<char> > > >::sort(struct std::greater<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >)' : cannot convert parameter 1 from 'bool (const class std::basic_string<char,struct std::char_trait
s<char>,class std::allocator<char> > &,const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)' to 'struct std::greater<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
>'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.

LISTSORT.exe - 1 error(s), 0 warning(s)
panchao521 2010-01-08
  • 打赏
  • 举报
回复
我早就这样写过,可惜还是报错啊。
例如
#include <string>
#include <list>
#include <iostream>
using namespace std;
struct cmp
{
bool operator () (const string& a,const string& b)
{
return a.size() < b.size();
}
};

int main(int argc, char* argv[])
{
list<string> list_story;

list_story.push_back("the");
list_story.push_back("quick");
list_story.push_back("red");
list_story.push_back("fox");
list_story.push_back("jumps");
list_story.push_back("over");
list_story.push_back("the");
list_story.push_back("slow");
list_story.push_back("red");
list_story.push_back("turtle");

list_story.sort(cmp());
return 0;
}


--------------------Configuration: LISTSORT - Win32 Debug--------------------
Compiling...
LISTSORT.cpp
E:\workspace\LISTSORT\LISTSORT.cpp(32) : error C2664: 'void __thiscall std::list<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,
class std::allocator<char> > > >::sort(struct std::greater<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >)' : cannot convert parameter 1 from 'struct cmp' to 'struct std::greater<class std::basic_string<cha
r,struct std::char_traits<char>,class std::allocator<char> > >'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.

LISTSORT.exe - 1 error(s), 0 warning(s)
FancyMouse 2010-01-08
  • 打赏
  • 举报
回复
真那啥。刚发现list::sort不接受functor只接受函数指针。。。
刚才那个别看。直接写个bool cmp(const string&,const string&)然后list_story.sort(cmp);就可以了。
FancyMouse 2010-01-08
  • 打赏
  • 举报
回复
string的<已经被重载过了。用一个functor去搞。
struct cmp { bool operator () (const string& a,const string& b) { return a.size() < b.size(); };

list_story.sort(cmp());
yshuise 2010-01-08
  • 打赏
  • 举报
回复
对于string不需要。另外是对“<”,而不是“>”
panchao521 2010-01-08
  • 打赏
  • 举报
回复
第一次提问,还没写明白呢就发出了。
对sort()排序默认是字典顺序。我想更改排序的规则。
查了一些网页,说是对“>”进行重载,可是还是报错啊。
那位仁兄能给个经典的案例?或是针对我的这个帮忙写两句
yshuise 2010-01-08
  • 打赏
  • 举报
回复
对于string不需要自定义。
yshuise 2010-01-08
  • 打赏
  • 举报
回复
list_story.sort();
加载更多回复(3)

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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