百分list sort的简单问题

Coder李海波 小米 研发工程师  2007-12-14 10:50:00
实在麻烦了,拜托说具体点,我刚用STL,手头又没有书。《The C++ programming language》在这方面一带而过。
就这个例子:
struct Dog
{
int age;
};
假如现在有个
int main()
{
list <Dog> dogList;
//...dogList.push_back(a lot of dogs);
//接下来要根据age排序,怎么弄啊
}
搞定了立马结贴。
...全文
111 点赞 收藏 13
写回复
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Coder李海波 2007-12-14
感谢楼上各位。
回复
batistawjp 2007-12-14
这个么,自定义个比较的函数
回复
SimonFu 2007-12-14
代码贴上....


struct Dog
{
int age;
Dog(int i):age(i){};
};

bool dogAgeLess(const Dog &a, const Dog &b)
{
return (a.age<b.age)?true:false;
}

int main()
{
list<Dog> dogs;
Dog dog1(1);
Dog dog2(5);
Dog dog3(3);
Dog dog4(6);
dogs.push_back(dog1);
dogs.push_back(dog2);
dogs.push_back(dog3);
dogs.push_back(dog4);
dogs.sort(dogAgeLess);

for(list<Dog>::iterator it = dogs.begin(); it!=dogs.end(); it++)
cout<<(*it).age<<endl;
}


回复
SimonFu 2007-12-14
自己写个比较Dog中age的函数或函数对象,带入list的sort函数,OK
回复
Vitin 2007-12-14
就是雁南飞说的.
回复
Vitin 2007-12-14

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

struct Dog
{
int age;
};

bool is_younger(const Dog& left, const Dog& right)
{
return left.age < right.age;
}

int main()
{
using std::list;
using std::sort;
using std::cout;
using std::endl;

list<Dog> dogList;
Dog d;
for (int i = 0; i < 40; i++)
{
d.age = (i*173)%89+1;
dogList.push_back(d); // 插入一个age=(i*173)%89+1的Dog
}
dogList.sort(is_younger); // 按age对dogList排序

for (list<Dog>::const_iterator iter = dogList.begin(); iter !=dogList.end(); ++iter)
{
cout << iter->age << " "; // 输出所有的age
}

system("pause");
}


需要注意的是,对std::list不能调用std::sort,而应使用std::list自己的sort函数.
回复
飞哥 2007-12-14
只要提供个比较函数,使用list自带的就行了吧,重载个比大小的
回复
Eat_By_Cpp 2007-12-14
其实如果你有msdn的话,很简单的
stl毕竟是类库集合,要你用脑袋记住很难的,参考msdn咯
呵呵
回复
believefym 2007-12-14
写个比较的函数,参数是Dog,
这个函数作为sort的第三个参数
bool Dogelder( const Dog& d1, const Dog& d2 )
{
return d1.age> d2.age;
}

sort(....,Dogelder);
回复
Kelinsman_SG 2007-12-14

///用链表插入排序
UINT LinkInsertSort(const Dog& st)
{
list <Dog> ::iterator iter = dogList.begin();
UINT pos = 0;
for (; iter != dogList.end(); iter++, pos++)
{
if (st.age > (*iter).age)
{
dogList.insert(iter, st);
return pos;
}
}

dogList.insert(dogList.end(), st);
return dogList.size();
}
回复
believefym 2007-12-14
参考一下这个,差不多的

// alg_sort.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional> // For greater<int>( )
#include <iostream>

// Return whether first element is greater than the second
bool UDgreater ( int elem1, int elem2 )
{
return elem1 > elem2;
}

int main( )
{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1;

int i;
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( 2 * i );
}

int ii;
for ( ii = 0 ; ii <= 5 ; ii++ )
{
v1.push_back( 2 * ii + 1 );
}

cout << "Original vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;

sort( v1.begin( ), v1.end( ) );
cout << "Sorted vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;

// To sort in descending order. specify binary predicate
sort( v1.begin( ), v1.end( ), greater<int>( ) );
cout << "Resorted (greater) vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;

// A user-defined (UD) binary predicate can also be used
sort( v1.begin( ), v1.end( ), UDgreater );
cout << "Resorted (UDgreater) vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
}
回复
Kelinsman_SG 2007-12-14
用链表插入排序
UINT LinkInsertSort(const Dog& st)
{
list<Dog>::iterator iter = dogList.begin();
UINT pos = 0;
for (; iter != dogList.end(); iter++, pos++)
{
if (st.age > (*iter).age)
{
dogList.insert(iter, st);
return pos;
}
}

dogList.insert(dogList.end(), st);
return dogList.size();
}
回复
Coder李海波 2007-12-14
难道这个问题真的这么难么?
回复
发动态
发帖子
C++ 语言
创建于2007-09-28

5.9w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
社区公告
暂无公告