0
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);
有時當我這樣做:
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));
}
我得到了o.owner =細胞[O] - >所有者,(printf的顯示不同的值),並且如果我在分配之前先打印單元格[o],它將返回非零指針,但該映射關鍵字不存在。
我使用gcc-4.6。
什麼是錯的代碼?
ADD1: 更改爲
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);
}
ADD2: 的最後一個版本的作品(我希望):
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);
}
五月是一些補充?
我寧願說只有三個表達式的單應該在任何時候是正確的。 –
@ mike.dld是的,這是更嚴格的條件必須保持。我修改了。 – Bathsheba
我是否必須設置operator