I am trying to use qsort
from STL to sort array of edge:
struct edge
{
int w,v,weight;
};
by weight. What I am trying is:
int compare_e(const void *a, const void *b)
{
return ( *(edge *)a->weight - *(edge *)b->weight );
};
But I get:
`const void*' is not a pointer-to-object type
EDIT: Ok thx, now my code is compiled but sort seems don't work 100%...
#include <cstdlib>
#include <iostream>
struct edge
{
int w,v,weight;
};
struct edge_ID:edge
{
int id;
};
int compare_e(const void *a, const void *b)
{
return ( ((edge *)a)->weight > ((edge *)b)->weight );
};
int main()
{
using namespace std;
edge *tab = new edge[100];
for(int i = 0; i < 100; i++)
{
tab[i].weight = rand() % 100;
cout << i << " => " << tab[i].weight << endl;
}
qsort(tab, 100, sizeof(edge), compare_e);
cout << "AFTER:" << endl;
for(int i = 0; i < 100; i++)
{
cout << i << " => " << tab[i].weight << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
I have some number in wrong place...