12.11 SORTING CLASS-TYPE OBJECTS BY OVERLOADING THE OPERATOR


12.11 SORTING CLASS-TYPE OBJECTS BY OVERLOADING THE ‘<’ OPERATOR

In the previous section, we showed how we can use a function object to tell a sorting routine how to compare two class type objects. An alternative approach consists of overloading the ‘<’ operator as we show in this section.

Shown below is an example where we have defined a Cat class and then provided a global overload definition for the operator ‘<’ for this class in line (A) of the program. With the operator overload definition in place, a call to the usual sort for a list<Cat> will automatically sort the Cat objects.

 
//SortWithLessThan.cc #include <string> #include <list> using namespace std; class Cat { string name; int age; public: Cat( string nam, int yy) : name(nam), age( yy ) {} string getName() const { return name; } int getAge() const { return age; } friend bool operator<( const Cat& x1, const Cat& x2 ); }; bool operator<( const Cat& x1, const Cat& x2 ) { //(A) return x1.age < x2.age; } template<class T> void print ( list<T> ); int main() { Cat kitty1( "socks", 6 ); Cat kitty2( "cuddles", 3 ); Cat kitty3( "tabby", 8 ); list<Cat> kittyList; kittyList.push_back( kitty1 ); kittyList.push_back( kitty2 ); kittyList.push_back( kitty3 ); kittyList.sort(); print( kittyList ); return 0; } template<class T> void print( list<T> li ) { typedef list<T>::const_iterator CI; for ( CI iter = li.begin(); iter != li.end(); iter++ ) cout << iter->getName() << " " << iter->getAge() << endl; cout << endl << endl; }

Sorting class-type objects by overloading the ‘<’ operator will not work on a container of pointers to user-defined types. Suppose, instead of storing Cat objects in a container of type vector<Cat>, we store Cat* pointers in a vector<Cat*> container. Now we could try to overload the ‘<’ operator in the following manner:

      bool operator<( const Cat* x1, const Cat* x2 ) {       // WRONG         return x1->age < x2->age;      } 

and declare this definition to be a friend of Cat by

      friend bool operator<( const Cat* x1, const Cat* x2 ); // WRONG 

This does not work because the compiler will insist that the arguments to the overloading of ‘<’ be class-type objects and not pointers to class type objects. This is in accord with the constraints on operator overloading listed in the introduction to this chapter.

So how does one sort a container of pointers to user-defined class-type objects? By going back to the functor based approach of the previous section. Shown below is an example that illustrates this. Note that for the sake of variety this implementation uses the vector container class and the generic library sort, since, unlike list, the class vector does not come equipped with a sort function of its own. Additionally, we have defined Cat_Comparator as a nested class.

 
//SortPointerTypes.cc #include <string> #include <vector> #include <algorithm> using namespace std; class Cat { string name; int age; public: class Cat_Comparator { public: bool operator() ( const Cat* x1, const Cat* x2 ) const { return x1->age < x2->age; } }; Cat( string nam, int yy) : name(nam), age( yy ) {} string getName() const { return name; } int getAge() const { return age; } friend bool Cat_Comparator::operator() ( const Cat* x1, const Cat* x2 ) const; }; template<class T> void print( vector<T> ); int main() { Cat* kitty1 = new Cat( "cuddles", 3 ); Cat* kitty2 = new Cat( "tabby", 8 ); Cat* kitty3 = new Cat( "socks", 6 ); vector<Cat*> kittyVec; kittyVec.push_back( kitty1 ); kittyVec.push_back( kitty2 ); kittyVec.push_back( kitty3 ); sort( kittyVec.begin(), kittyVec.end(), Cat::Cat_Comparator() ); print( kittyVec ); // cuddles 3 socks 6 tabby 8 } template<class T> void print( vector<T> li ) { typedef vector<T>::const_iterator CI; for ( CI iter = li.begin(); iter != li.end(); iter++ ) cout << (*iter)->getName() << " " << (*iter)->getAge() << " "; cout << endl << endl; }




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net