I am trying to sort the "sth" integer value within the vector of class Entry using qsort. The code for the same is as follows. But after applying qsort, also the values remain the same. When I tried to print the values in cmpfunc2(), I found that 0 0 is getting printed.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Entry{public:
int id;
int sth;
Entry(int,int);
};
Entry::Entry(int a,int b){
id=a;
sth=b;
}
int cmpfunc2 (const void * a, const void * b)
{
cout<<(*(Entry *)a).sth<<" "<<(*(Entry *)b).sth <<endl;
return ( (*(Entry*)a).sth - (*(Entry*)b).sth );
}
int main(){
vector<Entry> entries;
entries.push_back(Entry(2,3));
entries.push_back(Entry(21,14));
entries.push_back(Entry(54,12));
qsort(&entries, entries.size(),sizeof(Entry),cmpfunc2);
for(int i=0;i<entries.size();i++)
cout<<entr