头文件wuyong.h:
#pragma once #include<iostream> #include<vector> #include<deque> #include<algorithm> #include<string> using namespace std; class Player { public: Player(){} Player(string name, int score) :mName(name), mScore(score){} ~Player(){} public: string mName; int mScore; }; void Creat_Player(vector<Player>& v) { string nameSeed ="ABCDE"; for (int i = 0; i < 5; i++) { Player p; p.mName = "选手"; p.mName += nameSeed[i]; p.mScore = 0; v.push_back(p); } } void printscore(int val) { cout << val << " "; } void Set_score(vector<Player>& v) { for (vector<Player>::iterator it = v.begin(); it != v.end(); it++) { //对当前学生进行打分 deque<int> dScore; for (int i = 0; i < 10; i++) { int score = rand() % 41 + 60; dScore.push_back(score); } sort(dScore.begin(), dScore.end()); //for_each(dScore.begin(), dScore.end(),printscore); //cout << endl; //去除最高分和最低分 dScore.pop_front(); dScore.pop_back(); int totlescore = 0; for (deque<int>::iterator dit = dScore.begin(); dit != dScore.end(); dit++) { totlescore += (*dit); int aveScore = totlescore / dScore.size(); //保存分数 (*it).mScore = aveScore; } } } bool mycompare(Player& p1,Player& p2) { return p1.mScore > p2.mScore; } //分数排名 sort默认从小到大,希望从大到小 void Print_Rank(vector<Player>& v) { sort(v.begin(), v.end(), mycompare); for (vector<Player>::iterator it = v.begin(); it != v.end(); it++) { cout << "姓名:" << (*it).mName << " 得分: " << (*it).mScore << endl; } }
源文件wuyong.cpp:
#include"wuyong.h" int main() { vector<Player> vPlist; Creat_Player(vPlist); Set_score(vPlist); Print_Rank(vPlist); system("pause"); return 0; }