#include #include #include using namespace std ; class student { public: int id ; student( ) { id=0 ; } student( int idP ) { id = idP ; } student( const student& obj1 ) { id = obj1.id ; } ~student( ) { } }; bool studentCompareFunction (student obj1, student obj2 ) { return ( obj1.id < obj2.id); } class studentCompareClass { public: bool ascend = true ; bool operator() (student obj1, student obj2 ) { if ( ascend ) return ( obj1.id < obj2.id) ; else return ( obj2.id < obj1.id) ; } } ; int main() { student studentObject1(1) ; student studentObject2(2) ; student studentObject3(3) ; vector< student > vec ; vec.push_back( studentObject1 ) ; vec.push_back( studentObject3 ) ; vec.push_back( studentObject2 ) ; studentCompareClass studentCompareClassObj1 ; studentCompareClassObj1.ascend = false ; sort( vec.begin() , vec.end() , studentCompareClassObj1 ) ; for( student x1 : vec ) cout << x1.id << " " ; cout << endl ; vec.clear() ; vec.push_back( studentObject1 ) ; vec.push_back( studentObject3 ) ; vec.push_back( studentObject2 ) ; sort( vec.begin() , vec.end() , studentCompareFunction ) ; for( student x1 : vec ) cout << x1.id << " " ; cout << endl ; return 0 ; }