我遇到问题。声明中说,比赛结果是从标准输入中读取的,我必须按照解决问题的数量以递减顺序在屏幕上打印最终排名。这是我的代码。在C++中对结构向量排序
#include <cstdio>
#include <vector>
#include <cstdlib>
using namespace std;
struct results
{
unsigned int id; //id of the team
unsigned int m; //number of solved problems
};
int comparare(const void * i, const void * j) //compare function for qsort()
{
return -(*(unsigned int*)i - *(unsigned int*)j);
}
int main()
{
unsigned int n;
vector<results> standings; //initializing an array of structs
scanf("%u", &n); //the size of the vector
for(unsigned int i=0; i<n; ++i)
{
scanf("%u%u", &standings[i].id, &standings[i].m); //reading the elements
standings.push_back(results());
}
qsort(standings, n, sizeof(results), comparare); //sorting the array
for(unsigned int i=0; i<n; ++i)
printf("%u %u\n", standings[i].id, standings[i].m); //print the sorted array
return 0;
}
当我想编译代码,编译器发现错误
在该行不能转换“的std :: vector的”到“无效*”的说法“1”到“无效的qsort(无效*,为size_t,为size_t,__compar_fn_t)”
qsort(standings, n, sizeof(results), comparare);
我该怎么做才能修复这个
?
使用'std :: sort'。作为eric建议的 – erip
,请通过http://stackoverflow.com/questions/4708105/performance-of-qsort-vs-stdsort –