如何把排序函数用一个函数整合一下,但还是可以实现按各数据成员排序
怎么把排序函数用一个函数整合一下,但还是可以实现按各数据成员排序
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
const int n=2;
const int m=10;
class student{
private:
int ID,english,math,computer,total;
string name;
string sex;
int chinese;
public:
student(){cout<<"构造函数在执行\n";}
~student(){cout<<"析构函数在执行\n";}
student(student &p);
student &operator=(student&);
void Getinfo();
friend ostream&operator<<(ostream&os,const student &a);
friend istream&operator>>(istream&is,student&a);
void Print(student st[])const;
int FindID(student st[])const;
void FindName(student st[])const;
void SortName(student st[]);
void SortID(student st[],int (*fun)(int a,int b));
void SortTotal(student st[],int (*fun)(int a,int b));
void SortChinese(student st[],int (*fun)(int a,int b));
void SortMath(student st[],int (*fun)(int a,int b));
void SortEnglish(student st[],int (*fun)(int a,int b));
void SortComputer(student st[],int (*fun)(int a,int b));
void swap(student &,student &);
friend bool checki(string str);
};
void student::SortTotal(student st[n],int (*fun)(int a,int b))
{
int i,j,k; student s;
for(i=0;i<n-1;i++)
{ k=i;
for(j=i+1;j<n;j++)
if(fun(st[k].total,st[j].total)) {k=j;}
if(k!=i){s.swap(st[i],st[k]);}
}
for(i=0;i<n;i++)
cout<<st[i];
}
void student::SortID(student st[n],int (*fun)(int a,int b)) //fun是一个函数指针,用来选择升序、降序!
{
int i,j,k;
student s;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(fun(st[k].ID,st[j].ID)) {k=j;}
if(k!=i) {s.swap(st[i],st[k]); }
}
for(i=0;i<n;i++)
cout<<st[i];
}//这只是两个排序函数,我想把这些排序函数整合到一个函数中,但还是能实现按各数据成员排序!求建议!求实现方法!
------解决方案--------------------
http://en.cppreference.com/w/cpp/algorithm/sort
1.参考std::sort() 用函数对象.
2.给自己的sort 增加一个参数:成员指针.先 google :成员指针或 c++ primer-> 类->成员指针.
------解决方案--------------------
参考这个吧,用stl的sort,你只需要实现几个比较函数,按你的不同的类成员比较的函数,比如total, ID, math等.
http://www.cppblog.com/mzty/archive/2005/12/15/1770.html
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
const int n=2;
const int m=10;
class student{
private:
int ID,english,math,computer,total;
string name;
string sex;
int chinese;
public:
student(){cout<<"构造函数在执行\n";}
~student(){cout<<"析构函数在执行\n";}
student(student &p);
student &operator=(student&);
void Getinfo();
friend ostream&operator<<(ostream&os,const student &a);
friend istream&operator>>(istream&is,student&a);
void Print(student st[])const;
int FindID(student st[])const;
void FindName(student st[])const;
void SortName(student st[]);
void SortID(student st[],int (*fun)(int a,int b));
void SortTotal(student st[],int (*fun)(int a,int b));
void SortChinese(student st[],int (*fun)(int a,int b));
void SortMath(student st[],int (*fun)(int a,int b));
void SortEnglish(student st[],int (*fun)(int a,int b));
void SortComputer(student st[],int (*fun)(int a,int b));
void swap(student &,student &);
friend bool checki(string str);
};
void student::SortTotal(student st[n],int (*fun)(int a,int b))
{
int i,j,k; student s;
for(i=0;i<n-1;i++)
{ k=i;
for(j=i+1;j<n;j++)
if(fun(st[k].total,st[j].total)) {k=j;}
if(k!=i){s.swap(st[i],st[k]);}
}
for(i=0;i<n;i++)
cout<<st[i];
}
void student::SortID(student st[n],int (*fun)(int a,int b)) //fun是一个函数指针,用来选择升序、降序!
{
int i,j,k;
student s;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(fun(st[k].ID,st[j].ID)) {k=j;}
if(k!=i) {s.swap(st[i],st[k]); }
}
for(i=0;i<n;i++)
cout<<st[i];
}//这只是两个排序函数,我想把这些排序函数整合到一个函数中,但还是能实现按各数据成员排序!求建议!求实现方法!
------解决方案--------------------
http://en.cppreference.com/w/cpp/algorithm/sort
1.参考std::sort() 用函数对象.
2.给自己的sort 增加一个参数:成员指针.先 google :成员指针或 c++ primer-> 类->成员指针.
------解决方案--------------------
参考这个吧,用stl的sort,你只需要实现几个比较函数,按你的不同的类成员比较的函数,比如total, ID, math等.
http://www.cppblog.com/mzty/archive/2005/12/15/1770.html