c++之STL案例分析---员工分组

1,案例描述:

  • 公司今天招聘了10个员工(ABCDEFGHI),10名员工进入公司之后,需要指派员工在那个部门工作
  • 员工信息有: 姓名 工资组成;部门分为:策划、美术、研发
  • 随机给10名员工分配部门和工资
  • 通过multimap进行信息的插入 key(部门编号)value(员工)
  • 分部门显示员工信息

2 实现步骤

2.1.创建10名员工,放到vector中

1.先创建员工类:

class Worker
{
public:
	string m_Name;
	int m_Salary;
};

2.在main()函数中,创建10名员工的函数体代码:

//1.创建10名员工,放到vector中
vector<Worker>vWorker;
createWorker(vWorker);

2.2.谝历vector容器,取出每个员工,进行随机分组

3.在createWorker(vWorker)封装创建员工的函数谝历vector容器

// 2,进行随机分组
void createWorker(vector<Worker>& v)
{
	string nameSeed = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i++)
	{
		Worker worker;
		worker.m_Name = "员工";
		worker.m_Name += nameSeed[i];

		worker.m_Salary = rand() % 10000 + 10000; //10000~19999
		//将员工放入容器中
		v.push_back(worker);
	}
}

2.3.分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中

1,在主函数中创建map容器,将员工部门编号作为key,具体员工作为value,放入到multimap容

器中

//2,员工分组
multimap<int, Worker>mWorker;
setGroup(vWorker, mWorker);

2,将员工分组的函数进行封装

void setGroup(vector<Worker>&v,multimap<int,Worker>&m)
{
	for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
	{
		//产生随机部门编号
		int deptId = rand() % 3;//0~2

		//将员工插入到分组中
		//key部门编号,value为具体员工
		m.insert(make_pair(deptId, *it));
	}
}

随机生成部门编号后,我们可以在头文件宏定义:

策划部门编号为0

美术部门的编号为1

研发部门编号为2

#define CEHUA 0
#define MEISHU 1
#define YANFA 2

 

2.4.分部门显示员工信息

1,在主函数中创建显示分组员工的函数

	//3,显示分组员工
	showWorkerGroup(mWorker);

2,封装该函数

//4,显示分组员工
void showWorkerGroup(multimap<int,Worker>&m)
{
	//0 A B C 1 D E 2 F G ...
	cout << "策划部门: " << endl;

	multimap<int, Worker>::iterator pos = m.find(CEHUA);
	int count = m.count(CEHUA);//统计策划的人数
	int index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名;" << pos->second.m_Name << "\t" << "工资:"<< pos->second.m_Salary << endl;
	}

	//用同样的方法给其他部门统计
	cout << "--------------------------------------" << endl;
	cout << "策划部门: " << endl;
	pos = m.find(MEISHU);
	count = m.count(MEISHU);//统计策划的人数
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名;" << pos->second.m_Name << "\t" << "工资:" << pos->second.m_Salary << endl;
	}

	cout << "--------------------------------------" << endl;
	cout << "研发部门: " << endl;
	pos = m.find(YANFA);
	count = m.count(YANFA);//统计策划的人数
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名;" << pos->second.m_Name << "\t" << "工资:"  << pos->second.m_Salary << endl;
	}
}

代码解释:

先寻找策划部门的代码(0),然后利用count统计策划部门的人数,然后遍历策划部门的员工姓名和工资

2.5 在主函数插入随机数种子,让每次生成的效果的员工分组的情况和员工的工资都有变化

srand((unsigned)time(NULL));

3,代码的整体实现

#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<map>
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
#include<ctime>

/*公司今天招聘了10个员工(ABCDEFGHI),10名员工进入公司之后,需要指派员工在那个部门工作
员工信息有: 姓名 工资组成;部门分为:策划、美术、研发
随机给10名员工分配部门和工资
通过multimap进行信息的插入 key(部门编号)value(员工)
分部门显示员工信息*/

class Worker
{
public:
	string m_Name;
	int m_Salary;
};

// 2,进行随机分组
void createWorker(vector<Worker>& v)
{
	string nameSeed = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i++)
	{
		Worker worker;
		worker.m_Name = "员工";
		worker.m_Name += nameSeed[i];

		worker.m_Salary = rand() % 10000 + 10000; //10000~19999
		//将员工放入容器中
		v.push_back(worker);
	}
}

//员工分组
//3.分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
void setGroup(vector<Worker>&v,multimap<int,Worker>&m)
{
	for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
	{
		//产生随机部门编号
		int deptId = rand() % 3;//0~2

		//将员工插入到分组中
		//key部门编号,value为具体员工
		m.insert(make_pair(deptId, *it));
	}
}

//4,显示分组员工
void showWorkerGroup(multimap<int,Worker>&m)
{
	//0 A B C 1 D E 2 F G ...
	cout << "策划部门: " << endl;

	multimap<int, Worker>::iterator pos = m.find(CEHUA);
	int count = m.count(CEHUA);//统计策划的人数
	int index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名;" << pos->second.m_Name << "\t" << "工资:"<< pos->second.m_Salary << endl;
	}

	//用同样的方法给其他部门统计
	cout << "--------------------------------------" << endl;
	cout << "策划部门: " << endl;
	pos = m.find(MEISHU);
	count = m.count(MEISHU);//统计策划的人数
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名;" << pos->second.m_Name << "\t" << "工资:" << pos->second.m_Salary << endl;
	}

	cout << "--------------------------------------" << endl;
	cout << "研发部门: " << endl;
	pos = m.find(YANFA);
	count = m.count(YANFA);//统计策划的人数
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名;" << pos->second.m_Name << "\t" << "工资:"  << pos->second.m_Salary << endl;
	}
}

int main()
{
	srand((unsigned)time(NULL));

	//1.创建10名员工,放到vector中
	vector<Worker>vWorker;
	createWorker(vWorker);

	//2,员工分组
	multimap<int, Worker>mWorker;
	setGroup(vWorker, mWorker);

	//3,显示分组员工
	showWorkerGroup(mWorker);

	system("pause");
	return 0;
}

4,代码效果展示

 

相关推荐

  1. 基于SQL分析挖掘案例

    2024-04-11 14:14:01       50 阅读
  2. 如何分析和预测员工的绩效表现?

    2024-04-11 14:14:01       61 阅读
  3. react学习总结(二)案例分享

    2024-04-11 14:14:01       36 阅读
  4. py每日spider案例分享

    2024-04-11 14:14:01       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-11 14:14:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-11 14:14:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-11 14:14:01       82 阅读
  4. Python语言-面向对象

    2024-04-11 14:14:01       91 阅读

热门阅读

  1. 前端面试题大合集

    2024-04-11 14:14:01       33 阅读
  2. Vue项目Nginx配置自定义路径别名

    2024-04-11 14:14:01       36 阅读
  3. 头歌-机器学习 第14次实验 主成分分析PCA

    2024-04-11 14:14:01       37 阅读
  4. neo4j-01

    neo4j-01

    2024-04-11 14:14:01      38 阅读
  5. 【Ubuntu】在 Windows 和 Ubuntu 之间传输文件

    2024-04-11 14:14:01       35 阅读
  6. 简单几步启用Ubuntu root用户密码登录

    2024-04-11 14:14:01       32 阅读
  7. Gopher协议

    2024-04-11 14:14:01       40 阅读
  8. VUE的相关知识锦集

    2024-04-11 14:14:01       39 阅读
  9. redis

    redis

    2024-04-11 14:14:01      31 阅读
  10. 设计模式(016)行为型之命令模式

    2024-04-11 14:14:01       30 阅读