1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>
#include <string>
typedef struct S
{ std::string z ;
long l ;
} S ;
std::ostream& operator << ( std::ostream& os, const S& s )
{ std::cout << s.z << " " << s.l << std::endl ;
return os ;
};
class grLong
{ public:
grLong() {}
bool operator()(const S& v , const S& w ) { return v.l < w.l ; }
};
class grName
{ public:
grName() {}
bool operator()(const S& v , const S& w ) { return v.z < w.z ; }
};
int main()
{ S s ;
std::vector<S> v ;
s.z = "aaaa" ;
s.l = 10 ;
v.push_back( s ) ;
s.z = "zzz" ;
s.l = 90 ;
v.push_back( s ) ;
s.z = "ddd" ;
s.l = 11 ;
v.push_back( s ) ;
s.z = "cccc" ;
s.l = -9 ;
v.push_back( s ) ;
std::copy( v.begin(), v.end(), std::ostream_iterator<S>( std::cout, "") ) ;
std::cout << std::endl ;
std::sort( v.begin(), v.end(), grLong() ) ;
std::copy( v.begin(), v.end(), std::ostream_iterator<S>( std::cout, "") ) ;
std::cout << std::endl ;
std::sort( v.begin(), v.end(), grName() ) ;
std::copy( v.begin(), v.end(), std::ostream_iterator<S>( std::cout, "") ) ;
std::cout << std::endl ;
return 0 ;
} |
Partager