读书人

list排序,该怎么处理

发布时间: 2012-06-09 17:16:42 作者: rapoo

list排序

C/C++ code
#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排序的时候可以但是

就不行了

[解决办法]
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
[解决办法]
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/
[解决办法]
list有成员函数sort()可以排序
[解决办法]
是否是你要的代码?
C/C++ code
#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");}
[解决办法]
嗯,list不能用STL算法排序,无效的。成员函数有。
[解决办法]
能用sort()的必须支持RandomAccessIterator,而list的本性是sequential的,故而它本身提供了特别版的sort成员。
[解决办法]


RandomAccessIterator

是这个原因
[解决办法]
list 在内存中是不连续的
list有自己的成员函数 sort
不用算法中的排序
[解决办法]
受用,但是相对来说两者的具体用法,那位能解释下吗?谢谢!
[解决办法]

探讨
能用sort()的必须支持RandomAccessIterator,而list的本性是sequential的,故而它本身提供了特别版的sort成员。

[解决办法]

list 里面有这个方法的.
527 void sort();
[解决办法]
1楼已经解决了,随机迭代器
[解决办法]
对 随机迭代器

读书人网 >C++

热点推荐