list排序

hopesjd 2009-05-18 12:34:21
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
class Job
{
public:
int ID;
int time;
void set(int ID,int time)
{
this->ID=ID;
this->time=time;
}

void print()
{
cout<<"("<<ID<<","<<time<<")"<<endl;
}

};


bool comp_job( Job j1, Job j2)
{
return j1.time>j2.time;
}

void main()
{


vector<Job> x(3);
x[0].set(0,1);
x[1].set(1,5);
x[2].set(2,3);
//Job m;
list <Job> y;
list<Job>::iterator i;
y.push_back(x[0]);
y.push_back(x[1]);
y.push_back(x[2]);

sort(y.begin(),y.end(),comp_job);

for (i=y.begin();i!=y.end();i++)
{
i->print();
}
}

用vector排序的时候可以但是

就不行了
...全文
252 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
syp1989 2012-06-08
  • 打赏
  • 举报
回复
今天我被面试了 List排序问题,竟然没有答上来……
HelloDan 2009-05-18
  • 打赏
  • 举报
回复
The main drawback of lists compared to these other sequence containers is that they lack direct access to the elements by their position; For example, to access the sixth element in a list one has to iterate from a known position (like the beginning or the end) to that position, which takes linear time in the distance between these. They also consume some extra memory to keep the linking information associated to each element (which may be an important factor for large lists of small-sized elements).

可以用:list::sort

http://www.cplusplus.com/reference/stl/list/sort/
HelloDan 2009-05-18
  • 打赏
  • 举报
回复
template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );



<algorithm>

Sort elements in range

Sorts the elements in the range [first,last) into ascending order.

The elements are compared using operator< for the first version, and comp for the second.

Elements that would compare equal to each other are not guaranteed to keep their original relative order.


RandomAccessIterator
rwjlqn 2009-05-18
  • 打赏
  • 举报
回复
对 随机迭代器
nk_ysg 2009-05-18
  • 打赏
  • 举报
回复
1楼已经解决了,随机迭代器
once_and_again 2009-05-18
  • 打赏
  • 举报
回复

list 里面有这个方法的.
527 void sort();
Sou2012 2009-05-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ForestDB 的回复:]
能用sort()的必须支持RandomAccessIterator,而list的本性是sequential的,故而它本身提供了特别版的sort成员。
[/Quote]

UP
hongwei789007 2009-05-18
  • 打赏
  • 举报
回复
受用,但是相对来说两者的具体用法,那位能解释下吗?谢谢!
hopesjd 2009-05-18
  • 打赏
  • 举报
回复
大家都说了
每个人给点分吧
nuoshueihe 2009-05-18
  • 打赏
  • 举报
回复
list 在内存中是不连续的
list有自己的成员函数 sort
不用算法中的排序
  • 打赏
  • 举报
回复
RandomAccessIterator

是这个原因
ForestDB 2009-05-18
  • 打赏
  • 举报
回复
能用sort()的必须支持RandomAccessIterator,而list的本性是sequential的,故而它本身提供了特别版的sort成员。
adventurelw 2009-05-18
  • 打赏
  • 举报
回复
嗯,list不能用STL算法排序,无效的。成员函数有。
hjjdebug 2009-05-18
  • 打赏
  • 举报
回复
是否是你要的代码?

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
class Job
{
public:
int ID;
int time;
void set(int ID,int time)
{
this->ID=ID;
this->time=time;
}

void print()
{
cout<<"("<<ID<<","<<time<<")"<<endl;
}
friend bool operator < (Job j1, Job j2)
{
return j1.time < j2.time;
}
};


void main()
{


vector<Job> x(3);
x[0].set(0,1);
x[1].set(1,5);
x[2].set(2,3);
//Job m;
list <Job> y;
list<Job>::iterator i;
y.push_back(x[0]);
y.push_back(x[1]);
y.push_back(x[2]);

y.sort();

for (i=y.begin();i!=y.end();i++)
{
i->print();
}
system("pause");
}
xbs_l 2009-05-18
  • 打赏
  • 举报
回复
list有成员函数sort()可以排序

64,654

社区成员

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

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