在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第二十八篇博客。

本篇博客介绍了C++的map容器,并实现了员工分组。

本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。

目录

map容器

map基本概念

map构造和赋值

map大小和交换

map插入和删除

map查找和统计

map排序

员工分组


map容器

map基本概念

map里面所有元素都是pair。pair的第一个元素为键,起到索引作用,第二个元素为值。所有元素会根据键值自动排序。

mapmultimap属于关联式容器,底层结构用二叉树实现。map不允许键值重复,而multimap允许键值重复。使用map或multimap都要包含头文件map

map构造和赋值

map<T1,T2> mp创建一个map容器,键的类型为T1,值得类型为T2。

map(const map &mp)是拷贝构造函数。

map& operator=(const map &mp)重载等号操作符。

#include<iostream>
#include<map>
using namespace std;
int main(void)
{map<int, int> m1;m1.insert(pair<int, int>(25, 75));m1.insert(pair<int, int>(20, 20));m1.insert(pair<int, int>(10, 15));m1.insert(pair<int, int>(5, 20));m1.insert(pair<int, int>(15, 25));for (map<int, int>::iterator it = m1.begin(); it != m1.end(); ++it) {cout << it->first << "   " << it->second << endl;}cout << endl;map<int, int>m2(m1);for (map<int, int>::iterator it = m2.begin(); it != m2.end(); ++it) {cout << it->first << "   " << it->second << endl;}cout << endl;map<int, int>m3;m3 = m2;for (map<int, int>::iterator it = m3.begin(); it != m3.end(); ++it) {cout << it->first << "   " << it->second << endl;}cout << endl;return 0;
}

程序的输出是:

5   20
10   15
15   25
20   20
25   75

5   20
10   15
15   25
20   20
25   75

5   20
10   15
15   25
20   20
25   75

map大小和交换

size()返回容器中元素个数。

empty()判断容器是否为空。

swap(mp)交换两个容器。

#include<iostream>
#include<map>
using namespace std;
int main(void)
{map<int, int>m1;m1.insert(pair<int, int>(10, 100));m1.insert(pair<int, int>(20, 200));m1.insert(pair<int, int>(30, 300));m1.insert(pair<int, int>(40, 400));map<int, int>m2;m2.insert(pair<int, int>(60, 600));m2.insert(pair<int, int>(70, 700));m2.insert(pair<int, int>(80, 800));m2.insert(pair<int, int>(90, 900));cout << m1.empty() << endl;cout << m1.size() << endl;for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) {cout << it->first << "   " << it->second << endl;}cout << endl;for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {cout << it->first << "   " << it->second << endl;}cout << endl;m1.swap(m2);for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) {cout << it->first << "   " << it->second << endl;}cout << endl;for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {cout << it->first << "   " << it->second << endl;}cout << endl;return 0;
}

程序的输出是:

0
4
10   100
20   200
30   300
40   400

60   600
70   700
80   800
90   900

60   600
70   700
80   800
90   900

10   100
20   200
30   300
40   400

map插入和删除

insert(elem)将elem插入容器中。

clear()删除所有元素。

erase(pos)删除pos迭代器所指元素。

erase(beg,end)删除beg到end的所有元素,包括beg不包括end。

erase(key)删除键值为key的元素。

#include<iostream>
#include<map>
using namespace std;
int main(void)
{map<int, int>m;m.insert(pair<int, int>(10, 100));m.insert(pair<int, int>(20, 200));m.insert(make_pair(30, 300));m.insert(make_pair(40, 400));for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {cout << it->first << "   " << it->second << endl;}cout << endl;m.erase(30);for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {cout << it->first << "   " << it->second << endl;}cout << endl;return 0;
}

程序的输出是:

10   100
20   200
30   300
40   400

10   100
20   200
40   400

map查找和统计

find(key)查找key为键的是否存在,存在则返回指向的迭代器,否则返回.end。

count(key)返回key为键的元素个数。

map不能插入键重复的元素,否则以第一次为准。

#include<iostream>
#include<map>
using namespace std;
int main(void)
{map<int, int>m;m.insert(pair<int, int>(10, 100));m.insert(pair<int, int>(70, 700));m.insert(make_pair(60, 600));m.insert(make_pair(30, 300));map<int, int>::iterator it;it = m.find(50);if (it == m.end()) {cout << "not found" << endl;}else {cout << "found" << endl;}it = m.find(60);if (it == m.end()) {cout << "not found" << endl;}else {cout << "found" << endl;}cout << m.count(30) << endl;cout << m.count(20) << endl;return 0;
}

程序的输出是:

not found
found
1
0

map排序

对于自定义类型,要用仿函数指定排序规则。也可以用仿函数改变默认排序规则。

#include<iostream>
#include<map>
using namespace std;
class compare
{
public:bool operator() (int a, int b) const{return a > b;}
};
int main(void)
{map<int, int, compare> m;m.insert(pair <int, int>(30, 50));m.insert(pair <int, int>(40, 60));m.insert(make_pair(10, 20));m.insert(make_pair(25, 75));for (map<int, int, compare>::iterator it = m.begin(); it != m.end(); it++) {cout << it->first << "   " << it->second << endl;}return 0;
}

程序中用仿函数将排序规则改为降序。程序的输出是:

40   60
30   50
25   75
10   20

员工分组

公司招聘了十二个员工,进入公司后要指派在哪个部门工作。员工信息包括姓名和工资。部门有三个部分(程序中用ABC代替)。随机分配给员工部门和工资。

#include<iostream>
#include<map>
#include<vector>
#include<ctime>
using namespace std;class worker
{
public:string name;int salary;
};
int main(void)
{srand((unsigned int)time(NULL));vector<worker> v;worker w1;w1.name = "Linda";w1.salary = rand() % 5000 + 5000 + 1;worker w2;w2.name = "Larry";w2.salary = rand() % 5000 + 5000 + 1;worker w3;w3.name = "Lester";w3.salary = rand() % 5000 + 5000 + 1;worker w4;w4.name = "Lisa";w4.salary = rand() % 5000 + 5000 + 1;worker w5;w5.name = "Lidia";w5.salary = rand() % 5000 + 5000 + 1;worker w6;w6.name = "Lee";w6.salary = rand() % 5000 + 5000 + 1;worker w7;w7.name = "Lane";w7.salary = rand() % 5000 + 5000 + 1;worker w8;w8.name = "Leslie";w8.salary = rand() % 5000 + 5000 + 1;worker w9;w9.name = "Lorena";w9.salary = rand() % 5000 + 5000 + 1;worker w10;w10.name = "Lorenzo";w10.salary = rand() % 5000 + 5000 + 1;worker w11;w11.name = "Lowell";w11.salary = rand() % 5000 + 5000 + 1;worker w12;w12.name = "Laura";w12.salary = rand() % 5000 + 5000 + 1;v.push_back(w1);v.push_back(w2);v.push_back(w3);v.push_back(w4);v.push_back(w5);v.push_back(w6);v.push_back(w7);v.push_back(w8);v.push_back(w9);v.push_back(w10);v.push_back(w11);v.push_back(w12);multimap<int, worker> m;for (vector<worker>::iterator it = v.begin(); it != v.end(); ++it) {int work;work = rand() % 3;m.insert(make_pair(work, *it));}for (multimap<int, worker>::iterator it = m.begin(); it != m.end(); ++it) {if (it->first == 0) {cout << "A:" << "   ";}else if (it->first == 1) {cout << "B:" << "   ";}else if (it->first == 2) {cout << "C:" << "   ";}cout << "the name is " << it->second.name << "   " << "the salary is " << it->second.salary << endl;}return 0;
}

程序创建了worker类,其中name表示姓名,salary表示工资。随后创建了十二个worker类对象,并指定了姓名和工资。这里姓名是手动输入的,所以代码长了。工资是随机生成的,控制使得生成随机数的值是5001-10000。随后加入vector容器v。然后创建一个multimap容器m,键表示部门(0是A,1是B,2是C),值是worker类对象。然后遍历v,对于每个类对象,都将其作为值,将生成的0-2的随机数作为键加入m中。最后遍历容器m进行输出。

下面是程序的一次运行结果:

A:   the name is Lester   the salary is 8744
A:   the name is Lisa   the salary is 9206
A:   the name is Lidia   the salary is 8034
A:   the name is Lorenzo   the salary is 9951
A:   the name is Lowell   the salary is 6045
B:   the name is Lee   the salary is 6127
B:   the name is Leslie   the salary is 6978
B:   the name is Laura   the salary is 5622
C:   the name is Linda   the salary is 8658
C:   the name is Larry   the salary is 7003
C:   the name is Lane   the salary is 9832
C:   the name is Lorena   the salary is 8370

C++学习笔记(二十八)相关推荐

  1. 嵌入式系统设计师学习笔记二十八:嵌入式程序设计③——高级程序设计语言

    嵌入式系统设计师学习笔记二十八:嵌入式程序设计③--高级程序设计语言 解释程序和编译程序 编译器的工作阶段示意图 语法错误:非法字符,关键字或标识符拼写错误 语法错误:语法结构出错,if--endif ...

  2. Mr.J-- jQuery学习笔记(二十八)--DOM操作方法(添加方法总结)

    Table of Contents appendTo appendTo(source, target) 源代码 append prependTo ​ ​ ​ ​ prependTo源码 prepend ...

  3. 立创eda学习笔记二十八:在嘉立创购买pcb板并贴片(smt)

    完整的写一下,分为两部分: 1.下pcb订单 这个可以看之前写的一个博客: 立创eda学习笔记三:pcb购买_Gutie_bartholomew的博客-CSDN博客 补充一下,买pcb可以直接有几个途 ...

  4. OpenCV学习笔记(十六)——CamShift研究 OpenCV学习笔记(十七)——运动分析和物体跟踪Video OpenCV学习笔记(十八)——图像的各种变换(cvtColor*+)imgproc

    OpenCV学习笔记(十六)--CamShift研究 CamShitf算法,即Continuously Apative Mean-Shift算法,基本思想就是对视频图像的多帧进行MeanShift运算 ...

  5. OpenCV学习笔记(十八):凸包,最小包围区域算子:convexHull(),minAreaRect(),minEnclosingTriangle(),minEnclosingCircle()

    OpenCV学习笔记(十八):凸包,最小包围区域算子:convexHull(),minAreaRect(),minEnclosingTriangle(),minEnclosingCircle() 1. ...

  6. MATLAB学习笔记(十八)

    MATLAB学习笔记(十八) 一.Simulink仿真基础 1.1 Simulink的启动 1.2 系统仿真模型的创建 1.3 仿真参数的设置 1.4 总结 二.子系统的创建与封装 2.1 子系统的创 ...

  7. JavaScript学习(二十八)—事件冒泡和事件捕获

    JavaScript学习(二十八)-事件冒泡和事件捕获 一.什么是事件流? 简单说,事件流就是指事件的执行顺序,他包含两种模式:事件冒泡.事件捕获. (一).事件冒泡 最常用的一种模式,就是指事件的执 ...

  8. uniapp 学习笔记二十二 购物车页面结构搭建

    uniapp 学习笔记二十二 购物车页面结构搭建 cart.vue <template><view><view class="flex padding" ...

  9. Polyworks脚本开发学习笔记(二十)-补充几个常见操作指令的使用

    Polyworks脚本开发学习笔记(二十)-补充几个常见操作指令的使用 大概要写到结尾了,最后几篇就将手册的各常用命令再看一遍,组合一下,并列举出常见的一些有用的操作. DATA_COLOR_MAP数 ...

  10. 计算机二级C语言学习笔记(十八)

    上一篇:计算机二级C语言学习笔记(十七) 程序填空题(二) 题型二:平均值 首先求和,然后将和除以项数 ===================================== ========== ...

最新文章

  1. “拒绝在 iPad 上运行 Xcode!”
  2. 32位浮点数在威纶触摸屏显示_MCGS触摸屏与与西门子 S7-1200 PLC以太网通讯
  3. Spring中的InitializingBean的使用详解
  4. 平遥摄影展:卡农•布斯克茨和他的「一天的结束」
  5. 花里胡哨,不协调统一的界面,正如看到一个穿花里胡哨衣服的人,让人作呕,不想接近。...
  6. linux自定义服务
  7. 【排序算法】冒泡排序(C语言)
  8. 群晖NAS教程(十三)、利用Web Station安装wordpress博客
  9. python爬取微博评论_python爬虫手把手教你抓取微博评论(完整代码)
  10. 我的游戏测试面试过程
  11. python自动输入支付密码_python接入支付宝的实例操作
  12. 计算机科学关于人工智能的论文,人工智能论文
  13. python 图像快速替换某种颜色
  14. 钢铁侠材质制作——3、基础光照模型实现
  15. C语言单链表,能直接运行的代码!
  16. html搜索栏热搜效果,CSS3实战开发:百度新闻热搜词特效实战开发_html/css_WEB-ITnose...
  17. 一篇文章搞懂「低保真原型与高保真原型」
  18. Oracle 11g 数据库 实验7 数据库安全管理
  19. 秀外慧中雒芊芊:站在陈天桥背后的女人
  20. 郑州晚报百特联到尖山采摘“密二花”

热门文章

  1. 台达HMI和PC通信实现HMI程序的上传和下载常见错误总结与解决办法
  2. 如何用Python画一棵漂亮的树2.0
  3. 两万多字诠释python最经典基础算法之100题【内含思路、程序和答案】【python初学者必备】
  4. Revit二次开发高级应用(1)——利用wcf做简单的族库管理
  5. Java语言程序设计——学习笔记(辽宁大学,王青松主讲)
  6. 软件测试关键字搜索怎么测,软件测试中基于关键字的自动化测试
  7. [Javascript]诞生记
  8. Adobe Photoshop 2022安装教程(附安装包)
  9. 【软件相关】如何制作RSS源?
  10. 一个简单的美女拼图游戏