我遇到問題。聲明中說,比賽結果是從標準輸入中讀取的,我必須按照解決問題的數量以遞減順序在屏幕上打印最終排名。這是我的代碼。在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 –