My C++ is a little rusty as of late. Can one of you gurus help me define a SORT predicate, for a Container Class, with a template parameter which it self is a another class.
template <class Element>
class OrderedSequence
// Maintains a sequence of elements in
// ascending order (by "<"), allowing them to be retrieved
// in that order.
{
public:
// Constructors
OrderedSequence();
OrderedSequence(const OrderedSequence<Element>&);
// Destructor
~OrderedSequence(); // destructor
OrderedSequence<Element>& operator= (const OrderedSequence<Element>& ws);
// Get an element from a given location
const Element& get (int k) const;
// Add an element and return the location where it
// was placed.
int add (const Element& w);
bool empty() const {return data.empty();}
unsigned size() const {return data.size();}
// Search for an element, returning the position where found
// Return -1 if not found.
int find (const Element&) const;
void print () const;
bool operator== (const OrderedSequence<Element>&) const;
bool operator< (const OrderedSequence<Element>&) const;
private:
std::vector<Element> data;
};
So, this class receives a template parameter which is a STRUCT with std::string member variable.
I would like to define a simple sort predicate, so that I can call : std::sort(data.begin(), data.end(), sort_xx) after performing a : data.push_back() within the add() member function of the class above.
How do I do it? I am not using C++ 11 - just plain old C++.
Template parameter Element.. gets translated to:
struct AuthorInfo
{
string name;
Author* author;
AuthorInfo (string aname)
: name(aname), author (0)
{}
bool operator< (const AuthorInfo&) const;
bool operator== (const AuthorInfo&) const;
};
bool AuthorInfo::operator< (const AuthorInfo& a) const
{
return name < a.name;
}
bool AuthorInfo::operator== (const AuthorInfo& a) const
{
return name == a.name;
}