C++进阶实例2--员工分组

  1 #include<iostream>
  2 #include<map>
  3 #include<vector>
  4 #include<ctime>
  5 using namespace std;
  6
  7 #define CEHUA 0
  8 #define MEISHU 1
  9 #define YANFA 2
 10
 11 // 员工分组
 12 //
 13 // 案例描述:
 14 //    1. 10名员工(ABCDEFGHIJ)
 15 //    2. 员工信息:姓名,薪资组成;部门:策划、没熟、研发
 16 //    3. 随机给10名员工分配部门和薪资
 17 //    4. 通过multimap进行信息的插入,key(部门编号),value(员工)
 18 //    5. 分部门显示员工信息
 19 //
 20 // 解决思路:
 21 //    1. 创建10名员工,存入vector
 22 //    2. 遍历vector容器,取出每个员工,进行随机分组
 23 //    3. 分组后,将员工编号作为key,具体员工为value,存放到multimap容器中
 24 //    4. 分部门显示员工信息
 25 //
 26
 27 // 创建员工
 28 class Worker
 29 {
 30 public:
 31     string m_Name;
 32     int m_Salary;
 33 };
 34
 35 void createWorker(vector<Worker>& v) {
 36
 37     string nameSeed = "ABCDEFGHIJ";
 38     for (int i = 0; i < 10; i++) {
 39         Worker worker;
 40         worker.m_Name = "员工";
 41         worker.m_Name += nameSeed[i];
 42
 43         worker.m_Salary = rand() % 10000 + 10000;  // 10000 ~ 19999;
 44
 45         v.push_back(worker);
 46     }
 47
 48 }
 49
 50 // 员工分组
 51 void setGroup(vector<Worker>& v, multimap<int, Worker>& m) {
 52     for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++) {
 53         // 产生随机部门编号
 54         int depId = rand() % 3;  // 0, 1, 2
 55
 56         // 将员工插入到分组中
 57         // key表示部门编号,value表示具体员工
 58         m.insert(make_pair(depId, *it));
 59     }
 60 }
 61
 62 // 分组显式
 63 void showWorkerByGroup(multimap<int, Worker>&m) {
 64
 65     cout << "策划部们:" << endl;
 66     multimap<int, Worker>::iterator pos = m.find(CEHUA);
 67     int count = m.count(CEHUA);  // 统计具体人数
 68     int index = 0;
 69     for (; pos != m.end() && index < count; pos++, index++) {
 70         cout << "姓名:" << pos->second.m_Name << " 薪资:" << pos->second.m_Salary << endl;
 71     }
 72
 73     cout << "-----------------------" << endl;
 74     cout << "美术部门:" << endl;
 75     pos = m.find(MEISHU);
 76     count = m.count(MEISHU);  // 统计具体人数
 77     index = 0;
 78     for (; pos != m.end() && index < count; pos++, index++) {
 79         cout << "姓名:" << pos->second.m_Name << " 薪资:" << pos->second.m_Salary << endl;
 80     }
 81
 82     cout << "-----------------------" << endl;
 83     cout << "研发部门:" << endl;
 84     pos = m.find(YANFA);
 85     count = m.count(YANFA);  // 统计具体人数
 86     index = 0;
 87     for (; pos != m.end() && index < count; pos++, index++) {
 88         cout << "姓名:" << pos->second.m_Name << " 薪资:" << pos->second.m_Salary << endl;
 89     }
 90 }
 91
 92 void test01() {
 93
 94     // 随机数
 95     srand((unsigned int)time(NULL));
 96
 97     // 1.创建员工
 98     vector<Worker>vWorker;
 99     createWorker(vWorker);
100
101     // 测试员工信息
102     //for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++) {
103     //    cout << "姓名:" << it->m_Name << " 工资:" << it->m_Salary << endl;
104     //}
105
106     // 2.员工分组
107     multimap<int, Worker>mWorker;
108     setGroup(vWorker, mWorker);
109
110     // 3.分组显式员工
111     showWorkerByGroup(mWorker);
112 }
113
114 int main() {
115
116     test01();
117
118     system("pause");
119
120     return 0;
121 }
05-09 08:51