一、函数主体
- 创建员工
- 创建分组
- 分部门显示员工
二、设计一个员工类
有简单的员工姓名,工资。
class Worker
{
private:
string m_Name;
int m_Money;
};
三、实现创建员工
//创建员工
void creatWorker(vector<Worker>& v)
{
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
Worker worker;
worker.m_Name = "Person";
worker.m_Name += nameSeed[i];
worker.m_Money = rand() % 10000 + 100000;
v.push_back(worker);
}
//测试 是否成功写入 员工信息
//for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
//{
// cout << "姓名:" << it->m_Name << " " << "工资:" << it->m_Money << endl;
//}
}
四、实现员工分组
//设置分组
void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
{
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
{
int id = rand() % 3;
m.insert(make_pair(id, *it));
}
}
五、实现分部门显示
enum
{
CAIWU,RENLI,JISHU
};
//显示员工所在部门
void showWorker1(int n)
{
switch (n)
{
case 0:
cout << "CAIWU";
break;
case 1:
cout << "RENLI";
break;
case 2:
cout << "JISHU";
break;
}
cout << "部门的人员如下:" << endl;
}
void showWorker2(int count, multimap<int, Worker>& m, int n)
{
int index = 0;
multimap<int, Worker>::iterator pos = m.find(n);
for (; pos != m.end(), index < count; pos++, index++)
{
cout << "姓名:" << pos->second.m_Name << " " << "工资:" << pos->second.m_Money << endl;
}
}
//显示员工信息以及所在部门
void showWorker(multimap<int, Worker>m)
{
for (int i = 0; i < 3; i++)
{
showWorker1(i);
showWorker2(m.count(i), m, i);
}
}
六、总体实现
#define _CRT_SECURE_NO_WARNINGS;
/*
* 创建:CHENJING
* 日期:2022/11/6
* 主函数体:创建员工、员工分组、分部门显示
*/
#include <iostream>
using namespace std;
//#include"Worker.h"
#include<vector>
#include<map>
#include<string>
//枚举部门
enum
{
CAIWU,RENLI,JISHU
};
//员工类
class Worker
{
//创建员工
friend void creatWorker(vector<Worker>& v);
//显示员工信息
friend void showWorker2(int count, multimap<int, Worker>&m, int n);
private:
string m_Name;
int m_Money;
};
//创建员工
void creatWorker(vector<Worker>& v)
{
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
Worker worker;
worker.m_Name = "Person";
worker.m_Name += nameSeed[i];
worker.m_Money = rand() % 10000 + 100000;
v.push_back(worker);
}
//测试 是否成功写入 员工信息
//for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
//{
// cout << "姓名:" << it->m_Name << " " << "工资:" << it->m_Money << endl;
//}
}
//显示员工所在部门
void showWorker1(int n)
{
switch (n)
{
case 0:
cout << "CAIWU";
break;
case 1:
cout << "RENLI";
break;
case 2:
cout << "JISHU";
break;
}
cout << "部门的人员如下:" << endl;
}
void showWorker2(int count, multimap<int, Worker>& m, int n)
{
int index = 0;
multimap<int, Worker>::iterator pos = m.find(n);
for (; pos != m.end(), index < count; pos++, index++)
{
cout << "姓名:" << pos->second.m_Name << " " << "工资:" << pos->second.m_Money << endl;
}
}
//显示员工信息以及所在部门
void showWorker(multimap<int, Worker>m)
{
for (int i = 0; i < 3; i++)
{
showWorker1(i);
showWorker2(m.count(i), m, i);
}
}
//设置分组
void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
{
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
{
int id = rand() % 3;
m.insert(make_pair(id, *it));
}
}
int main()
{
vector<Worker>v;
//创建员工
creatWorker(v);
//创建分组
multimap<int, Worker>m;
setGroup(v, m);
//分部门显示员工
showWorker(m);
return 0;
}