C++ STL容器案例之员工分组

代码:

#include<iostream>
using namespace std;
#include<string>
#include<map>
#include<ctime>
#include<vector>

/**
* 
* Author:晚风亦是救赎
* CreateTime:2021/10/18
* 
* 案例:员工分组
*	案例描述:
*		1、公司招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在哪个部门工作
*		2、员工信息有:姓名  工资;部门分为:策划、美术、研发
*		3、随机给10名员工分配部门和工资
*		4、通过multimap进行信息的插入  key(部门编号)  value(员工)
*		5、分部门显示员工信息
*/

// 员工类
class Employee {
public:
	Employee(string name,int salary) {
		this->m_Name = name;
		this->m_Salary = salary;
	}
	string m_Name;    // 姓名
	int m_Salary;  // 薪水
};
// 部门类
class Department {
public:
	Department(string name, int no) {
		this->m_name = name;
		this->m_No = no;
	}
	string m_name; // 部门名字
	int m_No;      // 部门编号
};

// 仿函数实现 map 容器自定义排序
class MyCompare {
public:
	bool operator()(int v1,int v2)const{
		return v1 < v2;
	}
};

// 按照部门打印员工
void printEmployee(const multimap<int,Employee,MyCompare>&m,const vector<Department>&v,int depNo){
	cout << "下面打印:" << v.at(depNo).m_name << "的 成员信息!" << endl;
	for (multimap<int, Employee>::const_iterator it = m.begin(); it != m.end(); it++) {
		if (depNo == it->first) {
			cout << "部门:" << v.at(it->first).m_name << " ";
			cout << "姓名:" << it->second.m_Name << " ";
			cout << "薪水:" << it->second.m_Salary << endl;
		}
	}
	cout << endl;
}

void test() { 
	srand(time(0));

	// 部门信息初始化
	Department d1("策划部", 1);
	Department d2("美术部", 2);
	Department d3("研发部", 3);

	vector<Department>v;
	v.push_back(d1);
	v.push_back(d2);
	v.push_back(d3);

	string names[] = {"A","B","C","D","E","F","G","H","I","J"};
	multimap<int, Employee, MyCompare>m;
	int dep = 1;	   // 随机部门
	int salary = 0.0;  // 随机薪水
	for (int i = 0; i < 10; i++) {
		// 随机数初始化
		dep = rand() % 3;
		salary = rand() % 5000 + 1500;

		// 创建员工
		Employee temp(names[i], salary);
		pair<int, Employee> p(dep, temp);
		m.insert(p);
	}

	// 打印策划部成员信息
	printEmployee(m, v, 0);
	// 打印美术部成员信息
	printEmployee(m, v, 1);
	// 打印研发部成员信息
	printEmployee(m, v, 2);
}

int main() {
	test();
	system("pause");
	return 0;
}

(F_F) !

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值