Skip to content

Instantly share code, notes, and snippets.

@datayja
Created November 29, 2010 10:26
Show Gist options
  • Save datayja/719795 to your computer and use it in GitHub Desktop.
Save datayja/719795 to your computer and use it in GitHub Desktop.
Sample C++ class implementation of functor/comparer object
#include <string>
template <class T>
struct Comparer
{
Comparer(void){}
~Comparer(){}
// const & - not modifying compared items and not copying them
int operator () (const T & a, const T & b) const
{
return ( (a < b) ? -1 : ( (a > b) ? 1 : 0) );
}
int operator () (const T * a, const T * b) const
{
return ( (*a < *b) ? -1 : ( (*a > *b) ? 1 : 0) );
}
};
template <>
struct Comparer <std::string>
{
Comparer(void){}
~Comparer(){}
// const & - we do not want to make a copy or modify of the compared strings
int operator () (const std::string & a, const std::string & b) const
{
return a.compare(b);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment