Skip to content

Instantly share code, notes, and snippets.

@stevejarvis
Created August 15, 2014 14:28
Show Gist options
  • Save stevejarvis/f7562d6f761ef13c7028 to your computer and use it in GitHub Desktop.
Save stevejarvis/f7562d6f761ef13c7028 to your computer and use it in GitHub Desktop.
#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