Created
November 29, 2010 10:26
-
-
Save datayja/719795 to your computer and use it in GitHub Desktop.
Sample C++ class implementation of functor/comparer object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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