九度OJ1061成绩排序

来源:互联网 发布:surface laptop 知乎 编辑:程序博客网 时间:2024/05/18 04:05

九度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;}

0 0
原创粉丝点击