容器应用(vector,deque)

五名比赛选手,有10个评委打分,去除最低分与最高分,去平均分

#include
#include
#include
#include
#include
using namespace std;
//先创立一个选手类
class person
{
public:
	person(string name, int score)
	{
		this->m_Name = name;
		this->m_Score = score;
	}
	string m_Name;//姓名
	int m_Score;//平均分
};
void creat(vector& v)
{
	string names = "ABCDE";
	for (int i = 0; i < 5; i++)
	{
		string name = "选手";
		name += names[i];
		int score = 0;
		person p(name, score);
		v.push_back(p);
	}
}
void set(vector& v)
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		dequed;
		for (int i = 0; i < 10; i++)
		{
			int score = rand() % 41 + 60;//随机数种子,也可以自己输入分数
			d.push_back(score);
		}
		sort(d.begin(), d.end());//排序函数
		d.pop_back();
		d.pop_front();
		int sum = 0;
		for (deque::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			sum += *dit;
		}
		int avg = sum / d.size();
		it->m_Score = avg;
	}
}
void show(vector& v)
{
	for (vector::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << it->m_Name << it->m_Score << endl;
	}
}
int main()
{
	vectorv;
	creat(v);
	set(v);
	show(v);
	return 0;
}

你可能感兴趣的:(容器)