64,662
社区成员
发帖
与我相关
我的任务
分享
#include <list>
template<typename kk>
class myComp
{
public:
bool operator()(const kk& left,const kk& right)
{
return left > right;
}
};
int main()
{
std::list<int> iList;
list <int>::iterator c1_Iter;
iList.push_back( 20 );
iList.push_back( 10 );
iList.push_back( 30 );
for ( c1_Iter = iList.begin( ); c1_Iter != iList.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
iList.sort(myComp<int>());
for ( c1_Iter = iList.begin( ); c1_Iter != iList.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
return 0;
}
class Cmp
{
public:
bool operator () (int, int); //例如对两个 int 进行比较
};
#include "stdafx.h"
#include <iostream>
#include <list>
using namespace std;
class A{
public:
A(int m):a(m){}
bool operator <(const A& other)
{
return a < other.a;
}
friend std::ostream& operator <<(std::ostream& out, const A& obj){
out<< obj.a<<endl;
return out;
}
private:
int a;
};
int _tmain(int argc, _TCHAR* argv[])
{
list<A> S;
typedef list<A>::iterator it;
S.push_back(A(3));
S.push_back(A(2));
S.push_back(A(5));
S.sort();
it it1 = S.begin();
it it2 = S.end();
while(it1 != it2)
{
cout<<*it1<<endl;
it1++;
}
return 0;
}
class Nod
{
public:
int a;
int b;
bool operator < (const Nod& n)
{
return b < n.b;
}
};
int main(void)
{
list<Nod> ilist;
for (int i = 0; i < 10; ++i)
{
Nod tmp;
tmp.a = i;
tmp.b = 10-i;
ilist.push_back(tmp);
}
ilist.sort();
list<Nod>::iterator ite;
for (ite = ilist.begin(); ite != ilist.end(); ++ite)
{
cout << (ite->a) << " " << (ite->b) << endl;
}
return 0;
}
// list_sort.cpp
// compile with: /EHsc
#include <list>
#include <iostream>
int main( )
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter;
c1.push_back( 20 );
c1.push_back( 10 );
c1.push_back( 30 );
cout << "Before sorting: c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
c1.sort( );
cout << "After sorting c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
c1.sort( greater<int>( ) );
cout << "After sorting with 'greater than' operation, c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
}