《实用C++》第50课 C++容器之映射map

本文转载自:VC驿站

https://www.cctry.com/thread-291665-1-1.html

1、什么是 map,他跟 vector 和 list 有什么区别?什么情况下使用 map?
map提供的是一种键值对的容器,里面的数据元素都是成对出现的,即:key-value,在知道 key 的情况下能迅速的找到 value,他们是一一对应的关系。
如下图:第一个值称之为关键字(key),每个关键字只能在map中出现一次,是唯一的;第二个称之为该关键字的对应值:
《实用C++》第50课 C++容器之映射map 

例如:学校中的学生,用学生的什么信息作为 key 呢?可以用学号,在校的学生学号不可能重复,所以可以用学号作为key,学生的姓名或者学生信息作为value,value可以重复,可以相同,毕竟有重名的情况发生。

2、map 的使用场景:
优点查找起来很快:根据 key 值快速查找记录,查找的复杂度基本是 Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。怎么样快吧?所以,当大家以后的工程中有比较多的使用想快速查找的话可以使用map。

3、map的头文件和命名空间:
#include <map> //注意,没有 .h
using namespace std; //在 std 标准命名空间中

4、map的定义:
一般情况下,都是定义一个空的 map 对象,例如:
map<int, char> stud_sex_map;
定义完了之后再使用,map也是个模板类,所以他也是支持各种类型的。

5、map的使用:
定义完了之后我们就可以开始使用了,例如:

int main(int argc, char* argv[])
{
    map<int, char> stud_sex_map;
    stud_sex_map[10010] = 'm'; //赋值
    stud_sex_map[10011] = 'f';
    int n_size = stud_sex_map.size(); //返回容器的元素个数
    stud_sex_map.empty(); //检测容器是否为空

    char sex = stud_sex_map[10010];
    sex = stud_sex_map[10012]; //这个值会是多少呢?

    if (stud_sex_map.count(10012) <= 0) //判断容器中是否有某个元素
    {
        stud_sex_map[10012] = 'f';
    }

    sex = stud_sex_map[10012];

    return 0;
}

6、map的删除:
删除的话首当其冲的肯定是erase方法了。erase 方法支持 key 删除和迭代器删除,例如:
stud_sex_map.erase(10010);
stud_sex_map.erase(stud_sex_map.begin());

7、map的遍历:
因为是 map 不是数组,所以不能用下标来遍历,只能用迭代器来遍历,如下:
for (map<int, char>::iterator itor = stud_sex_map.begin(); itor != stud_sex_map.end(); ++itor)
{
    int key = itor->first;
    char ch = itor->second;
    cout << "key = " << key << ", value = " << ch << endl;
}

8、小作业:
定义一个 map<int, char> 类型的对象:
依次存入以下内容:
10010->'m'
10011->'f'
10012->'f'
10013->'m'
10014->'f'
10015->'m'

之后,使用 for 循环删除map中的value值为 'f' 的元素,并将结果输出出来!

第50课视频教程下载地址:

https://www.cctry.com/thread-291665-1-1.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
For some time I have wanted to write a Qt book that covered topics that were too advanced for C++ GUI Programming with Qt 4,★ even though that book itself has proved quite challenging for some readers. There is also some specialized material—not all of it difficult—that I wanted to cover that simply does not belong in a first book on Qt programming. Furthermore, in view of the sheer size of Qt, no one book can possibly do justice to all that it offers, so there was clearly room for the presentation of new material. What I’ve done in this book is to take a selection of modules and classes from a variety of areas and shown how to make good use of them. The topics chosen reflect both my own interests and also those that seem to result in the most discussion on the qt-interest mailing list. Some of the topics are not covered in any other book, while other topics cover more familiar ground—for example, model/view programming. In all cases, I have tried to provide more comprehensive coverage than is available elsewhere. So the purposes of this book are to help Qt programmers deepen and broaden their Qt knowledge and to increase the repertoire of what they can achieve using Qt. The “advanced” aspect often refers more to what you will be able to achieve than to the means of achieving it. This is because—as always—Qt insulates us as far as possible from irrelevant detail and underlying complexity to provide easy-to-use APIs that we can use simply and directly to great effect. For example, we will see how to create a music player without having to know anything about how things work under the hood; we will need to know only the high-level API that Qt provides. On the other hand, even using the high-level QtConcurrent module, the coverage of threading is necessarily challenging. This book assumes that readers have a basic competence in C++ programming, and at least know how to create basic Qt applications—for example, having read a good Qt 4 book, and having had some practical
目 录 第一章 开发环境 1.1 Qt 简介5 1.2 下载安装 Qt Creator 6 1.3 第一个程序 Hello World 7 第二章 窗体应用 1.1 窗体基类说明 12 1.2 控制窗体大小 13 1.3 窗体初始位置及背景色 13 1.4 修改标题栏图标 14 1.5 移动无边框窗体 16 1.6 去掉标题栏中最大化、最小化按钮 17 1.7 多窗体调用 18 1.8 字体形状窗体 20 第三章 控件应用 1.1 QPushButton按钮 23 1.2 QLabel标签 23 1.3 QLineEdit单行文本 24 1.4 QTextEdit多行文本 25 1.5 QPlainTextEdit多行文本 26 1.6 QComboBox下拉列表框 26 1.7 QFontComboBox字体下拉列表框 27 1.8 QSpinBox控件 28 1.9 QTimeEdit时间控件 29 1.10 QDateEdit日期控件 30 1.11 QScrollBar控件 30 1.12 QRadioButton单选按钮 31 1.13 QCheckBox复选框 32 1.14 QListView 列表控件 34 1.15 QTreeView树控件 34 1.16 QTableView表格控件 35 1.17 QHBoxLayout横向布局 36 1.18 QGridLayout网格布局 37 1.19 QGroupBox控件 38 1.20 QTabWidget控件 39 1.21 QMenu、QToolBar控件 41 1.22 任务栏托盘菜单 43 第四章 组件应用 1.1日历组件 47 1.2登录窗口 48 1.3文件浏览对话框 50 1.4颜色选择对话框 51 1.5进度条实例53 1.6Timer实时更新时间 54 第五章 文件操作 1.1创建文件夹 57 1.2写入文件 58 1.3修改文件内容 60 1.4删除文件 62 1.5修改文件名 63 1.6 INI文件写入操作 65 1.7 INI文件读取操作 68 1.8创建XML文件 71 1.9读取XML文件 72 第六章 图形图像操作 1.1绘制文字 75 1.2绘制线条 75 1.3绘制椭圆 77 1.4显示静态图像 78 1.5显示动态图像 78 1.6图片水平移动 79 1.7图片翻转 80 1.8图片缩放 82 1.9图片中加文字 84 1.10图像扭曲 85 1.11模糊效果 85 1.12着色效果 86 1.13阴影效果 87 1.14透明效果 87 第七章 多媒体应用 1.1音频、视频播放器 90 1.2播放Flash动画 94 1.3播放图片动画 95 第八章 系统操作 1.1获取屏幕分辨率 98 1.2获取本机名、IP地址 98 1.3根据网址获取IP地址 99 1.4判断键盘按下键值 100 1.5获取系统环境变量 101 1.6执行系统命令 102 第九章 注册表 1.0简要说明注册表 105 1.1写入注册表 105 1.2查找注册表 106 1.3修改IE浏览器的默认主页 107 第十章 数据库基础 1.1查询数据库驱动 109 1.2Qodbc连接Access数据库 109 1.3插入数据 111 1.4数据列表 112 1.5操作SQLite数据库 113 1.6SQLite数据库视图管理器 115 第十一章 网络开发 1.1点对点聊天服务端 119 1.2点对点聊天客户端 123 1.3局域网广播聊天 128 1.4SMTP协议发送邮件 148 1.5调用系统DLL判断网络连接状态 152 第十二章 进程与线程 1.1进程管理器 155 1.2线程QThread应用 158 1.3线程QRunnable应用 159 第十三章 数据安全 1.1 QByteArray加密数据 163 1.2 AES加密数据 164 1.3 MD5 加密数据 165 1.4 生成随机数 166 第十四章 打包部署 1.1 FilePacker 打包 169 1.2 Inno Setup 打包 174
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值