【C++】黑马C++泛型编程和STL技术 (9) STL常用容器 --- 容器使用案例

3.4 案例—评委打分

3.4.1 案例描述

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

3.4.2 实现步骤

  1. 创建5名选手,放到vector
  2. 遍历vector容器,取出来每一位选手,执行for循环,可以把10个评分存到deque容器中
  3. sort算法对deque容器中分数排序,去除最高分和最低分
  4. deque容器遍历一遍,累加总分
  5. 获取每位选手的平均分

代码实现:

/*有5名选手ABCDE,10个评委分别对每一名选手打分,去除最高分和最低分,取平均分。*/
#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<deque>
#include<algorithm>
#include<ctime>
// 创建选手类
class Competitor
{
public:
	Competitor(string name, int score)
	{
		this->m_Name = name;
		this->m_Score = score;
	}
	string m_Name;	// 姓名
	int m_Score;	// 均分
};
// 实例化5名选手
void createCompetitor(vector<Competitor>& v)
{
	string nameAppend = "ABCDE";
	for (int i = 0; i < 5; i++)		// 已经知道是5名选手,所以直接确定循环次数
	{
		string name = "选手";			// 选手名称的公共部分
		name += nameAppend[i];			// string容器的追加操作
		int score = 0;
		Competitor C(name, score);	// 创建选手
		v.push_back(C);					// 将选手放入到容器
	}
}
// 给5名选手打分
void setScore(vector<Competitor> &v)
{
	// 遍历---给每位选手打分
	for (vector<Competitor>::iterator vit = v.begin(); vit != v.end(); vit++)
	{
		deque<int> d;							// 创建容器存放得分
	// 10位评委打分
		for (int i = 0; i < 10; i++)
		{
			int score = rand() % 41 + 60;		// 60-100随机数
			d.push_back(score);					// 存入容器
		}
													// 测试
		/*cout << vit->m_Name << "的得分为:" << endl;
		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			
			cout << *dit << " ";
		}
		cout << endl;*/
	// 去除最高分和最低分
		sort(d.begin(), d.end());	// 排序
		d.pop_back();				// 去除最高分
		d.pop_front();				// 去除最低分
		// deque容器就是访遍这里的操作
	// 求平均分
		int sum = 0;
		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			sum += *dit;				// 求总分
		}
		int avg = sum / d.size();	// 求平均分
	// 将分数给到选手
		vit->m_Score = avg;
	}
}
// 显示得分
void showScore(vector<Competitor>& v)
{
	for (vector<Competitor>::iterator vit = v.begin(); vit != v.end(); vit++)
	{
		cout << vit->m_Name << "的得分为:"<< vit->m_Score << endl;
	}
}
int main()
{
	srand((unsigned int)time(NULL));	// 随机数种子
	// 一. 创建5名选手
	vector<Competitor> v;	// 1. 创建存放选手的vector容器
	createCompetitor(v);	// 2. 将选手的创建封装为函数
								// 3. 测试代码
	//for (int i = 0; i < v.size(); i++)
	//{
	//	cout << "姓名:" << v[i].m_Name << " " 
	//		<< "平均分:" << v[i].m_Score << endl;
	//}
	// 二. 给5名选手打分
	setScore(v);				// 4. 给容器内的每位选手打分
	// 三. 显示最后得分
	showScore(v);			// 5. 显示每位选手的最终得分
	system("pause");
	return 0;
}

3.10 案例—员工分组

3.10.1 案例描述

公司新入职10名员工ABCDEFGHIJ,员工信息包括姓名和工资。现在随机将这10名员工指派到策划、美术、研发三个部门去工作。请通过multimap容器进行信息的插入,key为员工部门编号,value为员工信息,并分部门显示员工信息。

3.10.2 实现步骤

  1. 创建10名员工,存放到vector容器中
  2. 遍历vector容器,取出每个员工,进行随机分组
  3. 分组后,将员工编号作为key,具体员工作为value,存放到multimap容器中
  4. 分部门显示员工信息

代码实现:

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<map>
#include<ctime>
#define CeHua 0
#define MeiShu 1
#define YanFa 2
// 创建员工类
class employee
{
public:
	employee(string name, int salary)
	{
		this->m_Name = name;
		this->m_Salary = salary;
	}
	string m_Name;
	int m_Salary;
};
// 创建员工
void createEmployee(vector<employee> &vEmployee)
{
	string nameAppend = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i++)
	{
		string name = "员工";
		name += nameAppend[i];
		int salary = rand() % 10000 + 10000; // 10000 - 19999
		employee e(name, salary);
		vEmployee.push_back(e);
	}
}
// 员工分组
void setGroup(vector<employee> &v, multimap<int, employee> &m)
{
	// 遍历每个员工
	for (vector<employee>::iterator it = v.begin(); it != v.end(); it++)
	{
		int deptId = rand() % 3;				// 0 1 2 随机生成部门编号
		m.insert(make_pair(deptId, *it));	// 将员工插入分组中
	}
}
// 按部门显示
// 插入的时候是按key值的大小升序的,key值对应的刚好是部门编号
// 如果知道每个部门编号的开始位置和该部门的人数就可以利用循环输出每个部门的员工信息
// 部门开始位置 ---> find函数按key值查找,返回所在位置迭代器
// 部门的人数 ---> count函数统计key出现的次数
void showEmployeeByDept(multimap<int,employee> &m)
{
	cout << "策划部门的员工为:" << endl;
	multimap<int, employee>::iterator pos = m.find(CeHua); //宏定义CeHua
	int num = m.count(CeHua);
	for (int i=0; pos != m.end() && i < num; pos++, i++)
	{
		cout << "姓名:" << pos->second.m_Name << " "
			<< "工资:" << pos->second.m_Salary << endl;
	}
	cout << "=======================" << endl;
	cout << "美术部门的员工为:" << endl;
	pos = m.find(MeiShu);
	num = m.count(MeiShu);
	for (int i = 0; pos != m.end() && i < num; pos++, i++)
	{
		cout << "姓名:" << pos->second.m_Name << " "
			<< "工资:" << pos->second.m_Salary << endl;
	}
	cout << "=======================" << endl;
	cout << "研发部门的员工为:" << endl;
	pos = m.find(YanFa); 
	num = m.count(YanFa);
	for (int i = 0; pos != m.end() && i < num; pos++, i++)
	{
		cout << "姓名:" << pos->second.m_Name << " "
			<< "工资:" << pos->second.m_Salary << endl;
	}
}
int main()
{
	srand((unsigned int)time(NULL));
	// 创建员工
	vector<employee> vEmployee;	// 1.vector容器存放员工
	createEmployee(vEmployee);	// 2.创建10名员工
										// 3.测试
	/*for(vector<employee>::iterator it=vEmployee.begin(); it!=vEmployee.end(); it++)
	{
		cout << "姓名:" << it->m_Name << " "
			<< "工资:" << it->m_Salary << endl;
	}*/
	// 员工分组
	multimap<int, employee> mEmployee;
	setGroup(vEmployee, mEmployee);
	// 按部门显示员工
	showEmployeeByDept(mEmployee);
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值