• 主页
• 问答

# 百分list sort的简单问题

Coder李海波 小米 研发工程师  2007-12-14 10:50:00

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

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");
}
``````

Eat_By_Cpp 2007-12-14

stl毕竟是类库集合，要你用脑袋记住很难的，参考msdn咯

believefym 2007-12-14

bool Dogelder( const Dog& d1, const Dog& d2 )
{
return d1.age> d2.age;
}

sort(....,Dogelder);

Kelinsman_SG 2007-12-14
``````
///用链表插入排序
{
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

{
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++ 语言

5.9w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等