Loading... ## 案例描述: 公司今天招聘10个员工,10名员工进入公司之后,需要指派员工在哪个部门工作 员工信息:姓名,工资组成 员工分为:策划,美术,研发 随机给10个员工分配部门和工资 通过multimap进行信息的插入 key (部门编号), value(员工) 分部门显示员工信息 ## 实现步骤: 1.创建10名员工,放入vector中 2.遍历vector容器,取出C每个员工,进行随机分组 3.分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中 4.分部门显示员工信息 ## 实现代码: ```cpp //1.创建10名员工,放入vector中 // //2.遍历vector容器,取出C每个员工,进行随机分组 // //3.分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中 // //4.分部门显示员工信息 #include<iostream> #include<vector> #include<string> #include<ctime> #include<map> using namespace std; #define CEHUA 0 #define MEISHU 1 #define YANFA 2 class Staff { public: Staff(string name, int wages) { this->M_name = name; this->M_wages = wages; } public: string M_name; int M_wages; }; void create_staff(vector<Staff>& sta) { string temp_name = "ABCDEFGHIJ"; for (int i = 0; i < 10; i++) { string m_name; m_name += temp_name[i]; int m_wages = rand() % 1000 + 2000; Staff p(m_name, m_wages); sta.push_back(p); } for (vector<Staff>::iterator it = sta.begin(); it != sta.end(); it++) { cout <<"姓名:"<< it->M_name <<" 工资:"<< it->M_wages << endl; } } void print_secion(multimap<int, Staff>&mul) { cout << "策划部门!"<<endl; multimap<int, Staff>::iterator it = mul.find(CEHUA); int sum = mul.count(CEHUA); for (int index = 0;it != mul.end()&& index < sum; index++,it++) { cout << "姓名:" << it->second.M_name << "工资:" << it->second.M_wages << endl; } cout << endl; cout << "美术部门!" << endl; it = mul.find(MEISHU); sum = mul.count(MEISHU); for (int index = 0; it != mul.end() && index < sum; index++, it++) { cout << "姓名:" << it->second.M_name << "工资:" << it->second.M_wages << endl; } cout << endl; cout << "研发部门!" << endl; it = mul.find(YANFA); sum = mul.count(YANFA); for (int index = 0; it != mul.end() && index < sum; index++, it++) { cout << "姓名:" << it->second.M_name << "工资:" << it->second.M_wages << endl; } cout << endl; } void create_section(vector<Staff>& sta) { multimap<int, Staff>mul; for (vector<Staff>::iterator it =sta.begin(); it != sta.end(); it++) { int num = rand()%3; mul.insert(make_pair(num, *it)); } print_secion(mul); } int main() { srand((unsigned int)time(NULL)); vector<Staff>sta; create_staff(sta); create_section(sta); return 0; } ``` 最后修改:2021 年 09 月 22 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果文章有用,请随意打赏。