Reputation: 41
I have
struct data_cell{
int owner;
std::vector<int> shares;
};
data_cell** data; //grid, very big
std::map<data_cell, data_cell*> cells; //all uniq cells
bool operator==(const data_cell &b, const data_cell &o) {
return (b.owner == o.owner && b.shares == o.shares);
}
bool operator<(const data_cell &b, const data_cell &o) {
return (b.owner < o.owner && b.shares != o.shares);
}
int to_grid(float, float);
sometimes when I do:
for (int i=0;i<ids.size();i++){//example
data_cell o=ids[i];
//some work where fills o.shares
data[to_grid(x,y)]=(cells[o]?:cells[o]=(new data_cell(o)));
printf("%d |%d|%d|%d %d\n", s.id, o.owner, cells[o]->owner, data[to_grid(x,y)]->owner, to_grid(x,y));
}
I got that o.owner != cells[o]->owner, (printf shows different values), and if I print cells[o] before assign it returns nonzero pointer, but this key doesn't exist in map.
I use gcc-4.6.
what is wrong with this code?
Add1: Change to
bool operator<(const data_cell &b, const data_cell &o) {
return (b.owner < o.owner && b.shares < o.shares);
}
doesn't help, but
bool operator<(const data_cell &b, const data_cell &o) {
return (b.owner < o.owner && b.shares <= o.shares);
}
Add2: The last version that works(I hope):
bool operator<(const data_cell &b, const data_cell &o) {
return (b.owner < o.owner && b.shares <= o.shares) ||
(b.owner <= o.owner && b.shares < o.shares);
}
May be some additions?
Upvotes: 0
Views: 54
Reputation: 234635
Your ==
and <
operators have to be such that exactly one of a < b
, a == b
, and b < a
is true
, and the other two false
. That is not the case here due to b.shares != o.shares
in your implementation of operator<
.
Upvotes: 3