这是要比较的字符串,lastname + firstName和id comp是公共的,我的排序方法不起作用,我无法找到代码失败的地方,我应该让学生在订单中列出
comp=new char[strlen(lName)+strlen(fName)+strlen(id)+1];
这是我的学生构造函数:
Student::Student(char * first, char * last, char *i,char * stand, int credit, double g, Date * d, Date * matricDate){
lName=last;
fName=first;
id=i;
standing=stand;
credits=credit;
gpa=g;
dob=d;
matDate=matricDate;
comp=new char[strlen(lName)+strlen(fName)+strlen(id)+1];
sprintf(comp,"%s%s%s",lName,fName,id);
};
这是我重载的运算符
bool Student::operator<(const Student &second){
if(comp<second.comp){
return true;
}else{
return false;
}
};
我有学生名单:
vector<Student *> roster_list;
我像这样添加学生:
void Roster::addStudent(Student *student){
roster_list.push_back(student);
};
这是我的排序:
void Roster::mySort(){
sort(roster_list.begin(),roster_list.end());
};
我的主要:
#include<iostream>
#include"Roster.cpp"
#include "Date.cpp"
#include "Student.cpp"
using namespace std;
int main(){
Roster *r=new Roster();
Date *d=new Date(12,05,1987);
Student *st=new Student("Jason", "Sam", "124542", "sophomore", 34, 4.0, d,d);
Student *st2=new Student("Ahmad", "Khan", "23452", "freshman", 34, 4.0, d,d);
Student *st3=new Student("Selam", "Can", "23431", "freshman", 34, 3.0, d,d);
Student *st4=new Student("Andrew", "Rosenberg", "34523", "Senior", 34, 4.0, d,d);
Student *st5=new Student("Selam", "Can", "23431", "freshman", 34, 3.0, d,d);
r->addStudent(st);
r->addStudent(st2);
r->addStudent(st3);
r->addStudent(st4);
// Student 3 and 5 are same
// == operator check
if(*st3==*st5){
cout<<"Students are same \n";
}
// != overloading operator check
if(*st3!=*st4){
cout<<"Students are different\n";
}
cout<<"################Before Sorting###############\n";
r->toString();
cout<<"################After Sorting###############\n";
r->mySort();
r->toString();
cout<<"\n"<<st->getComp();
cout<<"\n"<<st3->getComp();
return 0;
}
输出:
Students are same
Students are different
################Before Sorting###############
Student Info:Jason Sam 124542 sophomore 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Ahmad Khan 23452 freshman 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Selam Can 23431 freshman 3
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Andrew Rosenberg 34523 Senior 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
################After Sorting###############
Student Info:Jason Sam 124542 sophomore 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Ahmad Khan 23452 freshman 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Selam Can 23431 freshman 3
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Andrew Rosenberg 34523 Senior 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
SamJason124542
CanSelam23431
我试过这个
bool StudentSort(Student* lhs, Student* rhs) {
return (*lhs)<(*rhs);
}
void Roster::mySort(){
sort(roster_list.begin(),roster_list.end(),StudentSort);
};
仍然无法正常工作,我收到编译错误我有gcc版本4.4.7 20120313(Red Hat 4.4.7-3)(GCC)
这是我在尝试之后得到的错误:
Roster.cpp: In member function ‘void Roster::mySort()’:
Roster.cpp:69: error: argument of type ‘bool (Roster::)(Student*, Student*)’ does not match ‘bool (Roster::*)(Student*, Student*)’
答案 0 :(得分:2)
您的矢量/列表仅包含Student*
,而不包含Student
。
因此,您需要提供正确比较Student*
的Std :: sort。
就目前而言,默认排序可能是按内存位置排序,而不是指向对象的语义值。
// assumes operator< above,which it doesnt.
bool StudentSortFn(Student* lhs, Student* rhs) {
return (*lhs)<(*rhs);
}
当然,事实证明原始operator<
内嵌了大致相同的问题。它基于指针值(内存地址)比较char*
字段,而不是基于语义内容。
需要重写它才能使用strcmp或类似的东西:
bool Student::operator<(const Student &second){
return strcmp(comp,second.comp)<0;
};
答案 1 :(得分:1)
当你的向量是一个指针向量时,很难对学生进行排序。简单的方法是不用指针重写
vector<Student> roster_list;
void Roster::addStudent(Student student){
roster_list.push_back(student);
};
int main(){
Roster r;
Date d(12,05,1987);
Student st("Jason", "Sam", "124542", "sophomore", 34, 4.0, d,d);
Student st2("Ahmad", "Khan", "23452", "freshman", 34, 4.0, d,d);
Student st3("Selam", "Can", "23431", "freshman", 34, 3.0, d,d);
Student st4("Andrew", "Rosenberg", "34523", "Senior", 34, 4.0, d,d);
Student st5("Selam", "Can", "23431", "freshman", 34, 3.0, d,d);
r.addStudent(st);
r.addStudent(st2);
r.addStudent(st3);
r.addStudent(st4);
等。等