c++sort()函数对存放对象指针向量排序的问题
我想要用下面的代码实现对生生总成绩的排序但是编译不通过vector<Student*> stu;
bool cmp(Student* s1,Student* s2) //用来定义排序方式
{
return ((s1->Get_Sum_Score()) > (s2->Get_Sum_Score()));
}
void Sort_Class_Mates() { sort(stu.begin(), stu.end(), cmp); } //学生排名
报出如下错误:
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C3867 “Class::cmp”: 非标准语法;请使用 "&" 来创建指向成员的指针 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
错误 C3867 “Class::cmp”: 非标准语法;请使用 "&" 来创建指向成员的指针 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
错误 C2672 “sort”: 未找到匹配的重载函数 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
错误 C2672 “sort”: 未找到匹配的重载函数 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
错误 C2780 “void std::sort(const _RanIt,const _RanIt)”: 应输入 2 个参数,却提供了 3 个 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
错误 C2780 “void std::sort(const _RanIt,const _RanIt)”: 应输入 2 个参数,却提供了 3 个 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
我向问下这种用法对普通数组可以,怎么我这样不行??
有没有什么解决办法
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
struct student
{
int score;
};
void print(const student *pStu) {
cout << pStu->score << ' ';
}
bool cmp(const student *stu1, const student *stu2) {
return stu1->score < stu2->score;
}
int main(int, const char **)
{
srand(time(nullptr));
vector<student *> stu;
student *stu1 = new student;
student *stu2 = new student;
student *stu3 = new student;
stu1->score = rand() % 100;
stu2->score = rand() % 100;
stu3->score = rand() % 100;
stu.push_back(stu1);
stu.push_back(stu2);
stu.push_back(stu3);
for_each(begin(stu), end(stu), print);
cout << endl;
sort(begin(stu), end(stu), cmp);
for_each(begin(stu), end(stu), print);
cout << endl;
delete stu1;
delete stu2;
delete stu3;
stu1 = nullptr;
stu2 = nullptr;
stu3 = nullptr;
return 0;
} #include <iostream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
struct student
{
int score;
bool operator() (const student *stu1, const student *stu2) {
return stu1->score < stu2->score;
}
};
void print(const student *pStu) {
cout << pStu->score << ' ';
}
bool cmp(const student *stu1, const student *stu2) {
return stu1->score < stu2->score;
}
int main(int, const char **)
{
srand(time(nullptr));
vector<student *> stu;
student *stu1 = new student;
student *stu2 = new student;
student *stu3 = new student;
stu1->score = rand() % 100;
stu2->score = rand() % 100;
stu3->score = rand() % 100;
stu.push_back(stu1);
stu.push_back(stu2);
stu.push_back(stu3);
for_each(begin(stu), end(stu), print);
cout << endl;
sort(begin(stu), end(stu), student());
for_each(begin(stu), end(stu), print);
cout << endl;
delete stu1;
delete stu2;
delete stu3;
stu1 = nullptr;
stu2 = nullptr;
stu3 = nullptr;
return 0;
} 参考:http://www.cplusplus.com/reference/algorithm/sort/?kw=sort 本帖最后由 VOGHOST 于 2019-6-21 16:27 编辑
我就是个弟弟 发表于 2019-6-21 11:15
参考:http://www.cplusplus.com/reference/algorithm/sort/?kw=sort
谢谢哈,好了,把函数提到外面就好了
页:
[1]