写给自己
可以使用结构体内重载小于号,也可以使用编写cmp函数的形式
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct stu{
int niD;
string strName;
bool operator < (stu const& a) const {
if(niD < a.niD) return true;
if(niD == a.niD)
return strName.compare(a.strName) < 0;
return false;
}
}stu;
bool cmp(stu a,stu b){
if(a.niD > b.niD) return true;
if(a.niD == b.niD)
return a.strName.compare(b.strName) < 0;
return false;
}
int main(){
vector<stu> v;
stu a;
stu b;
a.niD=1;
a.strName="sfr";
b.niD=0;
b.strName="sfr1";
v.push_back(a);
v.push_back(b);
for(stu s:v)
cout<<s.niD<<endl;
//结构体内重载小于号
sort(v.begin(),v.end());
for(stu s:v)
cout<<s.niD<<endl;
//使用cmp函数
sort(v.begin(),v.end(),cmp);
for(stu s:v)
cout<<s.niD<<endl;
return 0;
}