公司今天招聘了10个员工 (ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作员工信息有:姓名工资组成;
部门分为: 策划、美术、研发;
随机给10名员工分配部门和工资;
通过multimap进行信息的插入 key(部门编号) value(员工);
分部门显示员工信息;
#include <iostream> #include <vector> #include <string> #include <map> #include <ctime> using namespace std; /** * 公司今天招聘了10个员工 (ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作员工信息有:姓名工资组成; 部门分为: 策划、美术、研发 随机给10名员工分配部门和工资 通过multimap进行信息的插入 key(部门编号) value(员工) 分部门显示员工信息 * */ #define CEHUA 0 #define MEISHU 1 #define YANFA 2 class Worker { public: Worker(string name, int salary) { this->m_Name = name; this->m_Salary = salary; } string m_Name; int m_Salary; }; void printVector(const vector<Worker> &v) { for (vector<Worker>::const_iterator it = v.begin(); it != v.end(); it++) { cout << "名字: " << (*it).m_Name << " 薪资:" << (*it).m_Salary << endl; } } void createWorker(vector<Worker> &vector1) { string nameSeed = "ABCEDFGHIJ"; for (int i = 0; i < 10; ++i) { string name = "员工:"; name += nameSeed[i]; int age = rand() % 2000 + 2000; Worker w(name, age); vector1.push_back(w); } } void setGroup(vector<Worker> &vector1, multimap<int, Worker> &multimap1) { int vSize = vector1.size(); for (int i = 0; i < vSize; ++i) { int deptId = rand() % 3; multimap1.insert(make_pair(deptId, vector1[i])); } } void printMultmap(multimap<int, Worker> &mWorker) { multimap<int, Worker>::iterator posCehua = mWorker.find(CEHUA); multimap<int, Worker>::iterator posMeishu = mWorker.find(MEISHU); multimap<int, Worker>::iterator posYanfa = mWorker.find(YANFA); cout << "策划" << endl; for (; posCehua != posMeishu; posCehua++) { cout <<"名字:"<< posCehua->second.m_Name <<"薪资:"<< posCehua->second.m_Salary << endl; } cout << "美术" << endl; for (; posMeishu != posYanfa; posMeishu++) { cout <<"名字:"<< posMeishu->second.m_Name <<"薪资:"<< posMeishu->second.m_Salary << endl; } cout << "研发" << endl; for (; posYanfa != mWorker.end(); posYanfa++) { cout <<"名字:"<< posYanfa->second.m_Name <<"薪资:"<< posYanfa->second.m_Salary << endl; } } int main() { //生成随机数 srand((unsigned int) time(NULL)); // 1、创建员工 vector<Worker> vWorker; createWorker(vWorker); printVector(vWorker); // 2、员工分组 multimap<int, Worker> mWorker; setGroup(vWorker, mWorker); printMultmap(mWorker); system("pause"); return 0; }