C++编程编程C++C++小练习-multimap容器应用
Beyondmultimap容器与vector容器应用
案列描述:
公司今天招聘了10个员工(ABCDEFHJ),10名员工进入公司之后分配到不同部门
部门有:”美术”,”研发”,”策划”
人员信息有: 姓名 工资 部门等组成
随机生成人员信息和分配部门
通过multimap进行信息的插入,key(部门),value(员工)
分部门显示员工信息
代码实现:
Staff员工类:
头文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#ifndef UNTITLED2_STAFF_H #define UNTITLED2_STAFF_H #include<iostream> using namespace std;
class Staff { private: string name; int salary; string department; public: Staff(); Staff(const string &name, int salary, const string &department);
const string &getName() const;
void setName(const string &name);
int getSalary() const;
void setSalary(int salary);
const string &getDepartment() const;
void setDepartment(const string &department); };
#endif
|
类实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include "Staff.h"
Staff::Staff() {}
Staff::Staff(const string &name, int salary, const string &department) : name(name), salary(salary), department(department) {}
const string &Staff::getName() const { return name; }
void Staff::setName(const string &name) { Staff::name = name; }
int Staff::getSalary() const { return salary; }
void Staff::setSalary(int salary) { Staff::salary = salary; }
const string &Staff::getDepartment() const { return department; }
void Staff::setDepartment(const string &department) { Staff::department = department; }
|
main函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include <iostream> #include <map> #include <vector> #include "Staff.h"
using namespace std;
void information(Staff &s) { int randomSalary = rand() % 25001 + 5000; s.setSalary(randomSalary); int randomDepartment = rand() % 3; char *department[] = {"策划", "美术", "研发"}; s.setDepartment(department[randomDepartment]); return; }
void creativeStaff(vector<Staff> &v) { string name = "ABCDEFGHIJ"; string name1 = "员工"; for (int i = 0; i < 10; i++) { Staff s; s.setName(name[i] + name1); information(s); v.push_back(s); } }
void printStaff(vector<Staff> &v) { for (Staff s: v) { cout << "姓名:" << s.getName() << '\t' << " 工资:" << s.getSalary() << '\t' << " 部门:" << s.getDepartment() << endl; } }
void distribution(multimap<string, Staff> &m, vector<Staff> &v) { for (Staff s: v) { m.insert({s.getDepartment(), s}); } }
void printByDepartment(multimap<string, Staff> &m) { char *department[] = {"策划", "美术", "研发"}; for (int i = 0; i < 3; i++) { cout << department[i] << "部门:" << endl; for (pair<string, Staff> s: m) { if (s.first == department[i]) { cout<<"姓名:"<<s.second.getName()<<'\t'<<"工资:"<<s.second.getSalary()<<endl; } } cout<<endl; } }
void test() { srand(time(NULL)); vector<Staff> v; creativeStaff(v); multimap<string, Staff> m; distribution(m, v); printByDepartment(m); }
int main() { test(); return 0; }
|
输出结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 策划部门: 姓名:A员工 工资:22028 姓名:B员工 工资:14367 姓名:D员工 工资:6268 姓名:I员工 工资:6497
美术部门: 姓名:E员工 工资:12154 姓名:H员工 工资:13239 姓名:J员工 工资:17478
研发部门: 姓名:C员工 工资:12070 姓名:F员工 工资:12187 姓名:G员工 工资:21004
|