C++进阶实例2--员工分组
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 }
C++进阶实例2--员工分组的更多相关文章
- 《Genesis-3D开源游戏引擎-官方录制系列视频教程:进阶实例篇》
注:本系列教程仅针对引擎编辑器:v1.2.2及以下版本 G3D进阶实例 第四课<2D编辑与脚本的统一入口> 使用G3D完成一个简单的类飞机大战游戏,介绍了G3D2d游戏制作的流程包括: ...
- [代码]multimap员工分组案例
案例要求: //multimap 案例//公司今天招聘了 5 个员工,5 名员工进入公司之后,需要指派员工在那个部门工作//人员信息有: 姓名 年龄 电话 工资等组成//通过 Multimap 进行信 ...
- C++ STL 之 multimap案例之员工分组
#include <iostream> #include <vector> #include <map> #include <string> #incl ...
- MySQL进阶5--分组函数 / 分组排序和分组查询 group by(having) /order by
MySQL进阶--分组排序和分组查询 group by(having) /order by /* 介绍分组函数 功能:用做统计使用,又称为聚合函数或组函数 1.分类: sum, avg 求和 /平均数 ...
- 01_dubbo实例_服务分组
[为什么要服务分组?] 当一个接口有多种实现时,可以用group区分. [ Provider 的配置信息] <?xml version="1.0" encoding=&quo ...
- mysql group by 与order by的实例分析(mysql分组统计后最大值)
CREATE TABLE `test` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) CHARACTER SET latin1 DEFAULT NULL, `c ...
- vue进阶 --- 实例演示
这篇博客将通过一个实例来对vue构建项目的过程有一个了解. 主要用到的知识点如下所示: vue-router 2.0路由配置 router-view 和 router-link的使用 transiti ...
- 《SQL 进阶教程》 自连接分组排序:练习题1-2-2
分组排序 SELECT d1.district, d1. NAME, (SELECT COUNT(d2.price) FROM district_products d2 WHERE d2.price ...
- Java JNI 编程进阶 实例+c++数据类型与jni数据类型转换
原文:http://www.iteye.com/topic/295776 JNI一直以来都很少去关注,但却是我心中的一个结,最近这几天刚好手头有点时间,因此抽空看了一下这方面的东西,整理了一份文档,J ...
随机推荐
- 为什么要用Spring
1.方便解耦,简化开发 通过Spring提供的IoC容器,我们可以将对象之间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合.有了Spring,用户不必再为单实例模式类.属性文件解析 ...
- 学习 Haproxy (五)
1 Linux Haproxy 负载均衡 v1.8 ★★★ 类似于ningx的反向代理1.1 Haproxy 概述 Haproxy是一个开源的高性能的反向代理或者说是负载均衡服务软件之一,它支持双机热 ...
- SpringCloud个人笔记-02-Feign初体验
项目结构 sb_cloud_product <?xml version="1.0" encoding="UTF-8"?> <project x ...
- 【推理引擎】如何在 ONNXRuntime 中添加新的算子
如果模型中有些算子不被ONNX算子库支持,我们就需要利用ONNXRuntime提供的API手动添加新算子.在官方文档中已经对如何添加定制算子进行了介绍(https://onnxruntime.ai/d ...
- ECMAScript中有两种属性:数据属性和访问器属性。
ECMA-262定义这些特性是为了实现JavaScript引擎用的,因此在JavaScript中不能直接访问它们.为了表示特性是内部值,该规范把它们放在了两对儿方括号中,例如 [[Enumerable ...
- html5的video元素学习手札
为了监控移动端视频播放的情况,研究了一下 html5 <video> 标签的属性与事件触发,及其在各系统和各个浏览器的兼容情况 属性与事件 理解清楚属性和事件,才能更好的使用 video ...
- cisco packet tracer安装步骤
一.进入Cisco Networking Academy Builds IT Skills & Education For Future Careers (netacad.com) 二.注册, ...
- RestTemplate-HTTP工具
RestTemplate 是由 Spring 提供的一个 HTTP 请求工具.在上文的案例中,开发者也可以不使用 RestTemplate ,使用 Java 自带的 HttpUrlConnection ...
- 文档声明(Doctype)和<!Doctype html>有何作用? 严格模式与混杂模式如何区分?它们有何意义?
文档声明的作用: 文档声明是为了告诉浏览器,当前HTML文档使用什么版本的HTML来写的,这样浏览器才能按照声明的版本来正确的解析. <!doctype html> 的作用就是让浏览器进入 ...
- 攻防世界——stegano
分析 1. 一个pdf,里边都是英文. 打开pdf "ctrl + F",检查flag 然活这里边直接告诉你,flag不在这里,一般都这么说了那就是真的不在了. 2. txt打开, ...