九度OJ的一个网友遇到一个小问题,随便看了看找了出来,学ACM好多代码是C/C++混编的,其实对C++并不是很了解,有时间看看重载大于号的机制到底是怎么回事
先学会怎么用。
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class StuInfo{
public:
string strName;
int nAge;
int nScore;
bool operator > ( const StuInfo &a ){
if( this->nScore > a.nScore )return true;
else if( this->nScore < a.nScore )return false;
else if( this->strName > a.strName )return true;
else if(this ->strName < a.strName )return false;
else if( this->nAge > a.nAge )return true;
else return false;
};
};
vector<StuInfo> StuList;
int cmp( StuInfo a, StuInfo b ){
if ( a > b )return 0;
else return 1;
}
int main( ){
int N;
StuInfo buffer;
while( cin >> N ){
StuList.clear();
for( int i = 0; i < N; i++ ){
cin >> buffer.strName >> buffer.nAge >> buffer.nScore;
StuList.push_back( buffer );
}
sort( StuList.begin(), StuList.end(), cmp );
for( vector<StuInfo>::iterator iter = StuList.begin(); iter < StuList.end(); ++ iter ){
cout << (*iter).strName << ' ' << (*iter).nAge << ' ' << (*iter).nScore <<endl;
}
}
return 0;
}