在C++中对结构向量进行排序
Sorting a vector of structs in 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 %un", standings[i].id, standings[i].m); //print the sorted array
return 0;
}
当我想编译代码时,编译器会发现错误
无法将参数"1"的"std::vector"转换为"void*"以将其转换为"void qsort(void*,size_t,size_tt,__compare_fn_t)"
在线路qsort(standings, n, sizeof(results), comparare);
中
我该怎么修?
如果你绝对必须在vector
上使用qsort
(而你没有。也不应该),那么你必须像这样传递它:
qsort(standings.data(), standings.size(), sizeof(results), comparare);
CCD_ 4获取指向存储在CCD_ 5中的阵列的指针。简单地将指针传递给vector
本身是没有帮助的。
注意,vector::data
需要C++11;如果您无法使用data
,请使用&vector[0]
。
但实际上,只需使用std::sort
:
std::sort(standings.begin(), standings.end(), [](const results &lhs, const results &rhs) {return lhs.id < rhs.id;});
显然,lambda需要C++11;对于早期的C++版本,可以随意使用命名空间声明的结构。
您正在使用C构造,但应该使用更多的C++构造。CCD_ 11通常比CCD_ 12快,并且它的使用更加直观。以下是如何在没有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;
}
这是一个表意符号的例子。
如果值可能超过INT_MAX
,则比较函数comparare
不适用。例如,比较UINT_MAX
和0
将导致在返回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);
}
如果*i
分别小于、等于或大于*j
,则返回-1
、0
或1
。
在C++中,还有其他更惯用的方法来对数组进行排序。
相关文章:
- 计算排序向量的向量中唯一值的计数
- 查找两个排序向量中共有的元素
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- 如何从C++的对的排序向量中获取有关给定值的相应对
- 对象接收堆栈溢出异常 c++ 的排序向量
- "x"的所有元素都存在于"y"(排序向量)中吗?
- 指向指针排序向量的指针向量的 C++ 向量
- 未排序向量上的lower_bound/upper_bound
- 智能指针的排序向量:神秘崩溃
- 按类型排序向量并按类型或派生类型搜索
- 如何在不使用标准算法的情况下在排序向量中添加 c 元素?
- C++排序向量<double>与在<Object>双成员变量上键控的向量
- 字符串指针的排序向量
- 具有前导数字的字符串的排序向量
- 保证重新排序向量
- 结构解释的C 排序向量
- 更新排序向量的一个条目
- 通过在排序向量上使用二叉搜索来定位未排序向量中的元素
- 排序向量上 std::find_if 和 std::bind2nd 的替代品
- 使用QuickSort算法以降序排序向量