Saturday, May 9, 2009

In C++, how do I use the STL sort command to sort a vector of template class objects?

Here is what I have in mind. It compiles, but will it work? Can't run the program yet.





Key is one of the private elements in the class, it's either an int or a string.





sort(imaVector.begin(Key), imaVector.end(Key))

In C++, how do I use the STL sort command to sort a vector of template class objects?
// Opless.cpp


// compile with: /EHsc


// Illustrates the defining the %26lt; operator to sort vectors


//


// Functions:


//


// operator%26lt; - Vector comparison operator.


//


// vector::begin - Returns an iterator to start traversal of the vector.


//


// vector::end - Returns an iterator for the last element of the vector.


//


// vector::iterator - Traverses the vector.


//


// vector::push_back - Appends (inserts) an element to the end of a


// vector, allocating memory for it if necessary.


//


// sort algorithm - Sorts the vector.


//


//////////////////////////////////////...





// The debugger can't handle symbols more than 255 characters long.


// STL often creates symbols longer than that.


// When symbols are longer than 255 characters, the warning is disabled.


#pragma warning(disable:4786)





#include %26lt;iostream%26gt;


#include %26lt;vector%26gt;


#include %26lt;string%26gt;


#include %26lt;algorithm%26gt;





using namespace std ;





// The ID class is used for team scoring. It holds each player's name


// and score.


class ID


{


public:


string Name;


int Score;


ID() : Name(""), Score(0) {}


ID(string NewName, int NewScore) : Name(NewName), Score(NewScore) {}


};





// In this example, an ID is equivalent only if both name and score match.


bool operator==(const ID%26amp; x, const ID%26amp; y)


{


return (x.Name == y.Name) %26amp;%26amp; (x.Score == y.Score);


}





// IDs will be sorted by Score, not by Name.


bool operator%26lt;(const ID%26amp; x, const ID%26amp; y)


{


return x.Score %26lt; y.Score;


}





// Define a template class for a vector of IDs.


typedef vector%26lt;ID%26gt; NAMEVECTOR;





int main()


{


// Declare a dynamically allocated vector of IDs.


NAMEVECTOR theVector;





// Iterator is used to loop through the vector.


NAMEVECTOR::iterator theIterator;





// Create a pseudo-random vector of players and scores.


theVector.push_back(ID("Karen Palmer", 2));


theVector.push_back(ID("Ada Campbell", 1));


theVector.push_back(ID("John Woloschuk", 3));


theVector.push_back(ID("Grady Leno", 2));





cout %26lt;%26lt; "Players and scores:" %26lt;%26lt; endl;


for (theIterator = theVector.begin(); theIterator != theVector.end();


theIterator++)


cout %26lt;%26lt; theIterator-%26gt;Score %26lt;%26lt; " "


%26lt;%26lt; theIterator-%26gt;Name %26lt;%26lt; endl;


cout %26lt;%26lt; endl;





// Sort the vector of players by score.


sort(theVector.begin(), theVector.end());





// Output the contents of the vector in its new, sorted order.


cout %26lt;%26lt; "Players ranked by score:" %26lt;%26lt; endl;


for (theIterator = theVector.begin(); theIterator != theVector.end();


theIterator++)


cout %26lt;%26lt; theIterator-%26gt;Score %26lt;%26lt; " "


%26lt;%26lt; theIterator-%26gt;Name %26lt;%26lt; endl;


}


Output


Players and scores:


2 Karen Palmer


1 Ada Campbell


3 John Woloschuk


2 Grady Leno





Players ranked by score:


1 Ada Campbell


2 Karen Palmer


2 Grady Leno


3 John Woloschuk


No comments:

Post a Comment