Когда я пытаюсь стереть элементы моей карты, это похоже на другой элемент, который стирается вместо этого. Я думал, что это плохой подход оператора <, но я не вижу проблемы.std :: map стереть неправильный элемент
inline bool operator<(const Etat &et){
if (et.x < this->x){
return false;
}
else if (et.x > this->x){
return true;
}
if (et.y < this->y){
return false;
}
else if (et.y > this->y){
return true;
}
if (et.ry < this->ry){
return false;
}
else if (et.ry > this->ry){
return true;
}
};
Etat конструктор:
Etat(x, y, ry, useless, useless);
и карта:
std::map< Etat, double > map;
//The 2 last parameters of Etat are useless
map.insert(std::pair< Etat, double >(Etat(2, 2, 2, 0, 0), 0.0));
//map.size() = 1
Etat e (0, 5 ,3, 0, 0);
map.erase(e);
//map.size() = 0
//Etat(2, 2, 2) is gone
У меня нет проблемы для всего экземпляра Etat в карте, только в этом случае, и некоторые другие.
испытания его дома:
#ifndef Etat_H
#define Etat_H
#include<iostream>
class Etat
{
public:
Etat(const int x, const int y, const int ry, const double vx, const double vy)
{
this->x = x;
this->y = y;
this->ry = ry;
this->vx = vx;
this->vy = vy;
};
Etat(){};
inline bool operator==(const Etat& et){
if (et.x == this->x && et.y == this->y && et.ry == this->ry && et.vx == this->vx && et.vy == this->vy){
return true;
}
return false;
};
inline bool operator!=(const Etat& et){
if (*this == et){
return false;
}
return true;
};
inline bool operator<(const Etat &et){
if (et.x < this->x){
return false;
}
else if (et.x > this->x){
return true;
}
if (et.y < this->y){
return false;
}
else if (et.y > this->y){
return true;
}
if (et.ry < this->ry){
return false;
}
else if (et.ry > this->ry){
return true;
}
};
inline bool operator>(const Etat& et){
if (*this < et){
return false;
}
return true;
};
inline const int getX() const {
return this->x;
};
inline const int getY() const {
return this->y;
};
inline const int getRY() const {
return this->ry;
};
private:
int x, y;
int ry;
double vx, vy;
};
#endif // !Etat_H
Маленький кусочек кода может воспроизвести проблему:
std::map< Etat, double > map;
map.insert(std::pair< Etat, double >(Etat(2, 2, 2, 0, 0), 0.0));
Etat e (0, 5 ,3, 0, 0);
map.erase(e);
ли вы на самом деле не получить [предупреждение] (http://coliru.stacked-crooked.com/a/0454caf5304f2b86) для этого кода? – chris
Если оба элемента равны, ни один из блоков 'if' не соответствует. – Barmar
ya Я знаю, потому что невозможно быть равным – Milow