0

I have a struct Point

struct Point
{
    int x;
    int y;
    bool operator == (const Point& point) const {
        return this->x != point.x ? false : (this->y != point.y ? false : true);
    }

    bool operator < (const Point& point) const {
        return this->x < point.x && this->y < point.y;
    }
};

I also have a DraughtType enum

enum class DraughtType {
    NORMAL, QUEEN
};

And I have a std::map < Point,DraughtType >. When I insert elements into std::map, only 3 values are inserted.

This is the code for inserting white draughts

void Board::initializeWhiteDraughts()
{
    for (auto i = 0; i < 3; ++i) {
        int x = i;
        for (auto j = i % 2 == 0 ? 0 : 1 ; j < COLUMNS; j+=2) {
            Point point;
            point.x = x;
            point.y = j;
            whiteDraughts[point] = DraughtType::NORMAL;
        }
    }
}

But the output is the following:

0 0
1 1
2 2

It's clearly missing a lot of draughts.

What's wrong with my code? I've tried to change the operator comparison, but it didn't work.

GianMS
  • 923
  • 2
  • 13
  • 25
  • 1
    `#include ` and `return std::tie(x, y) < std::tie(point.x, point.y);` – Retired Ninja Jun 25 '17 at 04:34
  • 2
    To be more specific about your code: your implementation of `operator <` is incorrect. Two objects are considered equivalent if both `a < b` and `b < a` are false, and `std::map` doesn't allow equivalent keys to exist in the map. Your definition of `<` allows many different objects to appear equivalent. – Mark Ransom Jun 25 '17 at 04:40
  • Further to @MarkRansom's answer - read about [strict weak ordering](https://en.wikipedia.org/wiki/Weak_ordering) – marko Jun 25 '17 at 19:57

0 Answers0