STL:deque容器小案例

有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低 分,取平均分

  1. 创建五名选手,放到vector中
  2. 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容 器中
  3. sort算法对deque容器中分数排序,pop_back pop_front去除最高和最低分
  4. deque容器遍历一遍,累加分数,累加分数/d.size()
  5. person.score = 平均分
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <time.h>
using namespace std;
class player
{
public:
	player(int score,string name)
	{
	  this->name = name;
	  this->score = score;
	}
	void Showme()
	{
	  cout<<name<<" "<<score<<endl;
	}
	int score;
	string name;
};
void init_player(vector<player>& v)
{
   string NameJi = "ABCDE";
   for(int i=0;i<5;i++)
   {
	   player tmp(0,string(1,NameJi[i]));
	   v.push_back(tmp);
   }
}
void start_player(vector<player>& v)
{
	srand((unsigned int)time(NULL));
	deque<int> d;
	for(vector<player>::iterator it = v.begin();it != v.end();it++)
	{
	   for(int i=0;i<10;i++)
	   {
	      d.push_back(60 + rand() % 41);
	   }
	   sort(d.begin(), d.end());
	   d.pop_back();
	   d.pop_front();
	   int sum = 0;
	   for (deque<int>::iterator it1 = d.begin(); it1 != d.end(); it1++)
	   {
			sum += *it1;
	   } 
	   sum/=d.size();
	   it->score = sum;
	   d.clear();
	}
}
void print(player& p)
{
    p.Showme();
}
int main()
{
	vector<player> v;
	init_player(v);
	for_each(v.begin(),v.end(),print);
	start_player(v);
	for_each(v.begin(),v.end(),print);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值