Hi all,
I have this really weird bug with STL set that I cannot understand.
My set function is given below.
struct pt{int a,b,node;}tmp,ins;
struct cmp{
bool operator()(pt a, pt b){
if(ans[a.node] != ans[b.node]) return ans[a.node] < ans[b.node];
if(a.node != b.node) return a.node<b.node;
if(a.a != b.a) return a.a<b.a;
return a.b<b.b;
}
};
set <pt,cmp> pq;
I am using this modified set for running dijkstra algorithm. However, I got some infinite loop somewhere. When I ran some debugging output, I noticed something extremely weird.
//Code portion with bug
while(!pq.empty()){
tmp = *pq.begin();
pq.erase(tmp);
printf("removed %d %d %d\n", tmp.node, tmp.a, tmp.b); //This node is removed.
if(pq.find(tmp) == pq.end()) printf("erased\n"); //This line is printed, which means the element is removed
printf("first element is now %d %d %d\n", pq.begin()->node, pq.begin()->a, pq.begin()->b);
//Amazingly, the first element is exactly the same as tmp! And I thought I already deleted it and cannot find it in the set...
}
Can someone tell me what is wrong? Is there something wrong with my comparison function or something that leads to the bug?
Thanks in advance.
I have this really weird bug with STL set that I cannot understand.
My set function is given below.
struct pt{int a,b,node;}tmp,ins;
struct cmp{
bool operator()(pt a, pt b){
if(ans[a.node] != ans[b.node]) return ans[a.node] < ans[b.node];
if(a.node != b.node) return a.node<b.node;
if(a.a != b.a) return a.a<b.a;
return a.b<b.b;
}
};
set <pt,cmp> pq;
I am using this modified set for running dijkstra algorithm. However, I got some infinite loop somewhere. When I ran some debugging output, I noticed something extremely weird.
//Code portion with bug
while(!pq.empty()){
tmp = *pq.begin();
pq.erase(tmp);
printf("removed %d %d %d\n", tmp.node, tmp.a, tmp.b); //This node is removed.
if(pq.find(tmp) == pq.end()) printf("erased\n"); //This line is printed, which means the element is removed
printf("first element is now %d %d %d\n", pq.begin()->node, pq.begin()->a, pq.begin()->b);
//Amazingly, the first element is exactly the same as tmp! And I thought I already deleted it and cannot find it in the set...
}
Can someone tell me what is wrong? Is there something wrong with my comparison function or something that leads to the bug?
Thanks in advance.