Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- class Coord {
- public:
- Coord ( int x, int y );
- bool operator< ( const Coord &r ) const;
- int x;
- int y;
- };
- Coord::Coord ( int x, int y ) : x(x), y(y)
- {
- }
- bool Coord::operator< ( const Coord &r ) const
- {
- return ( this->x < r.x && this->y < r.y );
- // Or you could try this
- /*
- return ( this->x < r.x || ( this->x == r.x && this->y < r.y ) );
- // if you try that, the output will be correct, but 2,51 will be smaller
- // than 77,37, which it isn't supposed to be.
- */
- }
- int main () {
- Coord test1(2,51);
- Coord test2(77,37);
- std::map<Coord, int> testmap;
- testmap[test1] = 1;
- testmap[test2] = 2;
- std::map<Coord, int>::iterator it;
- for(it=testmap.begin(); it!=testmap.end(); it++){
- std::cout << (it->first).x << "," << (it->first).y << ": ";
- std::cout << it->second << "\n";
- }
- // Result is:
- // 2,51: 2
- //
- // Nothing else.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement