Created
August 15, 2014 14:28
-
-
Save stevejarvis/f7562d6f761ef13c7028 to your computer and use it in GitHub Desktop.
This file contains 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 <iostream> | |
#include <map> | |
class Thing | |
{ | |
public: | |
explicit Thing( int i ) | |
{ m_i = i; } | |
bool operator==( const Thing & other ) const | |
{ return this->m_i == other.m_i; } | |
bool operator!=( const Thing & other ) const | |
{ return !( *this == other ); } | |
/** Without this < definition, fails to compile (at least with Clang, I | |
imagine GCC as well). And if it's logically wrong, it compiles but | |
fails to find the right item. **/ | |
bool operator<( const Thing & other ) const | |
{ return this->m_i < other.m_i; }; | |
int m_i; | |
}; | |
int main() { | |
Thing one = Thing( 1 ); | |
Thing two = Thing( 2 ); | |
Thing three = Thing( 3 ); | |
std::map<Thing, int> things; | |
things[ one ] = 1; | |
things[ two ] = 2; | |
things[ three ] = 3; | |
auto iter = things.find( two ); | |
if( iter == things.end() ) { | |
std::cout << "Not found" << std::endl; | |
} else { | |
std::cout << "Found " << iter->first.m_i << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment