Witam, to znowu ja. Napisałem sobie testowo kolejkę priorytetową o wielkości 10 elementów wypełnioną - pierwszy element 0 a reszta 999. Następnie chciałem zobaczyć jak kolejka będzie mi sie sortowała więc zmieniłem wartość elementów 1 i 5 tak jak poniżej w kodzie, tyle że kolejka niepoprawnie sortuje. Powinno być 0, 3, 10, 999, 999.... a jest 0, 3, 999, 10, 999, 999... tak jakby 999 < 10 ?!
class Graph_node
{
public:
long int ref;
double lat;
double lon;
double distance_from_source;
};
class XComparator {
public:
bool operator()(const Graph_node * node1, const Graph_node * node2);
};
bool XComparator::operator()(const Graph_node * node1, const Graph_node * node2)
{
return (node1->distance_from_source > node2->distance_from_source);
}
int main()
{
priority_queue<Graph_node*, vector<Graph_node*>, XComparator> p_queue;
Graph_node * tab[10];
for(int i=0; i<10; i++)
{
Graph_node * ob;
ob = new Graph_node;
ob->ref=i;
if(i==0) ob->distance_from_source =0;
else ob->distance_from_source=999;
p_queue.push(ob);
tab[ob->ref] = ob;
}
tab[5]->distance_from_source = 10;
tab[1]->distance_from_source = 3;
while(!p_queue.empty())
{
Graph_node * a;
a = p_queue.top();
cout<<a->distance_from_source<<endl;
p_queue.pop();
}
return 0;
}