【发布时间】:2014-01-13 09:49:30
【问题描述】:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct properties{
int index; // student's index number
string name; // name of student
int points; // points of exam
bool sorter(properties a, properties b){
return a.points < b.points;
}
};
int main()
{
properties students[6];
vector<int> v;
for(int i = 0; i < 6; i++){
cin >> students[i].index >> students[i].name >> students[i].points;
}
for(int i = 0; i < 6; i++){
v.push_back(students[i].points);
}
stable_sort(students.begin(), students.end(), sorter);
return 0;
}
我有以下程序,现在我必须扩展它以按从最高点到最低点的排序顺序打印元素。我需要最小和最简单的代码,因为在我的情况下时间不是问题。任何帮助表示赞赏。
更新:我收到两个错误:
error: expected primary-expression before ',' token
error: expected primary-expression before '+' token
在这一行:
sort(properties, properties + 5);
【问题讨论】:
-
@DCoder 我不明白这个例子......对不起
-
你试过
std::sort吗? -
@Erbureth 是的......但我不知道如何用 struct 来实现它......我以前用它来处理数组,它工作得很好,但我无法做到在这种情况下工作..
-
见
std::sort的第二种形式,它允许你指定你自己的比较函数。链接的文档包括示例实现。 -
你没有阅读文档