Point ist eine Struktur der Form:C++ arbeiten mit Karten von structs
typedef struct Point {
int x;
int y;
bool operator<(const Point &other) const
{
return ((this->x < other.x) && (this->y < other.y));
};
bool operator!=(const Point &other) const
{
return ((this->x != other.x) || (this->y != other.y));
}
bool operator==(const Point &other) const
{
return ((this->x == other.x) && (this->y == other.y));
}
} Point;
und ich bin mit:
map<Point,int> points;
die Karte mit {{0,0} initialisiert, 1 }. und das Programm hat points.count (p) verwendet, um zu überprüfen, ob der Punkt p ein Schlüssel in der Punktekarte ist.
Es gibt ein Problem und das Programm gibt immer ja zurück! sogar für Punkte, die nicht in der Karte sind. Ich meine, wenn p kein Schlüssel in Punkten ist, bekomme ich points.count (p) == 1 (und nicht 0).
Wenn ich points.find (p) verwende, um den Iterator zu überprüfen, ob der empfangene Punkt wirklich == 0 ist (es ist nicht), bekomme ich einen Verweis auf einen ganz anderen Punkt.
Irgendeine Idee Wie behebe ich das Problem?
Ihr 'Operator <' definiert nicht ein [* Strikte schwache Ordnung *] (https : //www.sgi.com/tech/stl/StrictWeakOrdering.html). – BoBTFish
Sie haben Recht, wenn ich Punkte mit Point = {0,0} und int = 1 initialisiere; Ich bekomme, dass {1,1} kein Schlüssel ist, während {0,1} ist .. wie repariere ich das ?. Edit: @BoBTFish hey Ich habe deinen Link verpasst, jetzt überprüfe ich das – Jayn
[Diese Frage hat Antworten für 3 Punkte] (http://stackoverflow.com/q/979759/1171191). – BoBTFish