Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3.  
  4. class Coord {
  5.     public:
  6.         Coord ( int x, int y );
  7.         bool operator< ( const Coord &r ) const;
  8.         int x;
  9.         int y;
  10. };
  11.  
  12. Coord::Coord ( int x, int y ) : x(x), y(y)
  13. {
  14. }
  15.  
  16. bool Coord::operator< ( const Coord &r ) const
  17. {
  18.     return ( this->x < r.x && this->y < r.y );
  19.     // Or you could try this
  20.     /*
  21.     return ( this->x < r.x || ( this->x == r.x && this->y < r.y ) );
  22.     // if you try that, the output will be correct, but 2,51 will be smaller
  23.     // than 77,37, which it isn't supposed to be.
  24.     */
  25. }
  26.  
  27. int main () {
  28.     Coord test1(2,51);
  29.     Coord test2(77,37);
  30.     std::map<Coord, int> testmap;
  31.     testmap[test1] = 1;
  32.     testmap[test2] = 2;
  33.     std::map<Coord, int>::iterator it;
  34.     for(it=testmap.begin(); it!=testmap.end(); it++){
  35.         std::cout << (it->first).x << "," << (it->first).y << ": ";
  36.         std::cout << it->second << "\n";
  37.     }
  38.     // Result is:
  39.     // 2,51: 2
  40.     //
  41.     // Nothing else.
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement