2015-12-05 112 views
0

我遇到问题。声明中说,比赛结果是从标准输入中读取的,我必须按照解决问题的数量以递减顺序在屏幕上打印最终排名。这是我的代码。在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);

我该怎么做才能修复这个

+6

使用'std :: sort'。作为eric建议的 – erip

+2

,请通过http://stackoverflow.com/questions/4708105/performance-of-qsort-vs-stdsort –

回答

5

如果你绝对必须在vector使用qsort(和你不应该),那么你必须通过这样的:

qsort(standings.data(), standings.size(), sizeof(results), comparare); 

vector::data取一个指向存储阵列在vector。简单地传递一个指向vector本身的指针将无济于事。

请注意vector::data需要C++ 11;如果data不适用于您,请使用&vector[0]

不过说真的,just use std::sort

std::sort(standings.begin(), standings.end(), [](const results &lhs, const results &rhs) {return lhs.id < rhs.id;}); 

显然拉姆达需要C++ 11;随意为更早的C++版本使用一个命名空间声明的结构体。

0

您正在使用C构造,但应该使用更多的C++构造。 std::sort通常比qsort快,它的使用更直观。以下是如何在没有C++ 11的情况下重写它的方法。

#include <iostream> 
#include <vector> 
#include <algorithm> 

struct results { 
    unsigned int id; //id of the team 
    unsigned int m; //number of solved problems 
}; 

// I guess you're trying to sort on number of solved problems. If not, change `.m` to `.id` 
bool comparare(const results lhs, const results rhs) { 
    return lhs.m > rhs.m; 
} 

int main() { 

    size_t n; 

    std::cout << "Enter number of results: " << std::endl; 
    std::cin >> n; 

std::vector<results> standings(n); // Creates std::vector of results with n elements 

    // read in id and number of problems solved 
    for(size_t i=0; i < n; ++i) { 
    std::cin >> standings[i].id >> standings[i].m; 
    } 

    // sort the array 
    std::sort(standings.begin(), standings.end(), comparare); 

    // output the sorted array's id 
    for(size_t i = 0; i < standings.size(); ++i) { 
    std::cout << "In " << i+1 << " place: " << standings[i].id << " with " << standings[i].m << " problems solved." << std::endl; 
    } 

    return 0; 
} 

以下是​​的例子。

0

如果值可能超过INT_MAX,那么您的比较函数comparare不适用。例如,比较UINT_MAX0将返回UINT_MAX - 0作为int会导致溢出。这是未定义的行为,在普通平台上它实际上是负面的。

使用该比较函数:

//compare function for qsort() 
int comparare(const void *i, const void *j) { 
    unsigned int ni = *(unsigned int*)i; 
    unsigned int nj = *(unsigned int*)j; 
    return (ni > nj) - (ni < nj); 
} 

它返回-101如果*i是分别小于,等于或大于*j更大。

在C++中还有其他更习惯的方法来对数组进行排序。