STL提供了4个关联容器:set、multiset、map和multimap。这些容器提供了通过keyword高速存储和訪问数据元素的能力。Set和map不同意有反复keyword,而multiset和multimap同意反复keyword。关联容器的几个共同函数例如以下:

find(key):搜索容器中具有指定keyword的元素,返回指向此元素的迭代器。

lower_bound(key):搜索容器中具有指定keyword的第一个元素。返回指向此元素的迭代器。

upper_bound(key):搜索容器中具有指定keyword的最后一个元素,返回指向此元素的迭代器。

count(key):返回容器中具有指定keyword的元素的数目。

Map是比較重要的STL容器。本文主要来介绍map的原理和一些常见的使用方法。

1.map实现的原理

Map内部自建一个一颗红黑树,一种严格意义上的平衡二叉树,这颗树具有对数据自己主动排序的功能。所以在map内部全部的数据都是有序的。

2.数据插入

1)用insert函数插入pair数据。

2)用insert函数插入value_type数据。

3)用数组方式插入数据。

以上三种方法,都能够实现插入操作,可是还是有差别的。用insert函数进行插入操作时,在数据的插入上涉及到集合的唯一性这个概念,即当map中有key值时。insert操作无法插入数据。可是假设是用数组方式就不同了,它能够覆盖曾经keyword相应的值。能够用pair来获得是否插入成功,Pair<map<int, string>::iterator, bool> Insert_Pair,运行完insert操作后。通过推断Insert_Pair.second值的真假来推断是否插入成功。

代码举例及分析例如以下:

#include <iostream>
#include <map>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
map<int,string> map1;
map1.insert(pair<int,string>(1,"John")); //insert pair的方式插入
map1.insert(pair<int,string>(1,"Jeff")); //不会再运行插入操作
map1.insert(map<int,string>::value_type(2,"King")); //insert value_type的方式插入
map1.insert(map<int,string>::value_type(2,"Jane")); //不会再运行插入操作
map1[3]="Peter"; //数组方式插入
map1[3]="Smith"; //会覆盖掉上一个值
map<int,string>::iterator p; //迭代器遍历输出
for(p=map1.begin();p!=map1.end();p++)
{
cout<<p->first<<"\t"<<p->second<<endl;
}
pair<map<int, string>::iterator, bool> insert_pair; //用pair推断是否成功插入数据
insert_pair=map1.insert(pair<int,string>(4,"Kevin"));
if(insert_pair.second) //推断是否插入成功
{
cout<<"Insert Kevin Successfully"<<endl;
}
else
{
cout<<"Insert Kevin failed"<<endl;
}
insert_pair=map1.insert(pair<int,string>(4,"Cathy"));
if(insert_pair.second)
{
cout<<"Insert Cathy Successfully"<<endl;
}
else
{
cout<<"Insert Cathy failed"<<endl;
}
for(p=map1.begin();p!=map1.end();p++)
{
cout<<p->first<<"\t"<<p->second<<endl;
}
return 0;
}

运行结果为:

1 John

2 King

3 Smith

Insert Kevin Successfully

Insert Cathy failed

1 John

2 King

3 Smith

4 Kevin



3.数据遍历

1)应用前向迭代器

2)应用反向迭代器

3)用数组方式

代码及分析例如以下:

#include <iostream>
#include <map>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
map<int,string> map1;
map1.insert(pair<int,string>(1,"John"));
map1.insert(pair<int,string>(2,"Jeff"));
map1.insert(pair<int,string>(3,"Smith"));
map<int,string>::iterator it; //前向迭代器遍历输出
cout<<"应用前向迭代器输出:"<<endl;
for(it=map1.begin();it!=map1.end();it++)
{
cout<<it->first<<"\t"<<it->second<<endl;
}
map<int,string>::reverse_iterator reverse_it; //反序迭代器
cout<<"应用反向迭代器输出:"<<endl;
for(reverse_it=map1.rbegin();reverse_it!=map1.rend();reverse_it++)
{
cout<<reverse_it->first<<"\t"<<reverse_it->second<<endl;
}
int size = map1.size();
cout<<"应用数组方法输出:"<<endl;
for(int index=1;index<=size;index++)
{
cout<<index<<"\t"<<map1[index]<<endl;
}
return 0;
}

运行结果为:

应用前向迭代器输出:

1 John

2 Jeff

3 Smith

应用反向迭代器输出:

3 Smith

2 Jeff

1 John

应用数组方法输出:

1 John

2 Jeff

3 Smith



4.数据查找

1)用count函数推断keyword是否出现,它的缺点是不能定位keyword的位置。

2)用find函数。它会返回一个迭代器,假设查找到数据,则返回数据所在位置的迭代器,假设没有查找到,则返回end迭代器。

代码及分析例如以下:

#include <iostream>
#include <map>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
map<int,string> map1;
map1.insert(pair<int,string>(1,"John"));
map1.insert(pair<int,string>(2,"Jeff"));
map1.insert(pair<int,string>(3,"Smith"));
map<int,string>::iterator it; //前向迭代器遍历输出
cout<<"应用前向迭代器输出:"<<endl;
for(it=map1.begin();it!=map1.end();it++)
{
cout<<it->first<<"\t"<<it->second<<endl;
}
if (map1.count(1)!=0) //用count函数来进行查找
{
cout<<"1 is in map"<<endl;
}
else
{
cout<<"1 is not in map"<<endl;
}
if(map1.find(4)!=map1.end()) //用find函数来进行查找
{
cout<<"4 is in map"<<endl;
}
else
{
cout<<"4 is not in map"<<endl;
}
return 0;
}

运行结果为:

应用前向迭代器输出:

1 John

2 Jeff

3 Smith

1 is in map

4 is not in map



5.map使用[ ]符号注意事项

使用[ ]对map进行插入或者查找操作很便捷。可是假设map下标符运用不得当,就会造成意想不到的问题。对于map而言,并没有下标越界的概念,可是却有可能发生keyword在map中不存在的问题。假设訪问的keyword在map中并不存在,则map会自己主动生成对应的keyword。并会给定一个默认的初始值。

代码和解析例如以下:

#include <iostream>
#include <map>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
map<int,string> map1;
map1.insert(pair<int,string>(1,"John"));
map1.insert(pair<int,string>(2,"Jeff"));
map1.insert(pair<int,string>(3,"Smith"));
if (map1[4] == "Kevin") //4不存在。map会自己主动加入。并初始化值为空。即“”
{
map1[4]="Cathy";
}
map<int,string>::iterator it; //前向迭代器遍历输出
cout<<"应用前向迭代器输出:"<<endl;
for(it=map1.begin();it!=map1.end();it++)
{
cout<<it->first<<"\t"<<it->second<<endl;
}
return 0;
}

运行结果为:

应用前向迭代器输出:

1 John

2 Jeff

3 Smith

4

如输出所看到的,最后输出4的值为空。

这是由于在进行查找操作时,map自己主动加入4为keyword。且它的值为空。所以。推断语句一直不成立。最后输出仍然为空。

【STL容器学习】-关联容器与map的用法的更多相关文章

  1. ###STL学习--关联容器

    点击查看Evernote原文. #@author: gr #@date: 2014-08-23 #@email: forgerui@gmail.com STL中的关联容器. ###stl学习 |--迭 ...

  2. stl中顺序性容器,关联容器两者粗略解释

    什么是容器 首先,我们必须理解一下什么是容器,在C++ 中容器被定义为:在数据存储上,有一种对象类型,它可以持有其它对象或指向其它对像的指针,这种对象类型就叫做容器.很简单,容器就是保存其它对象的对象 ...

  3. C++学习基础四——顺序容器和关联容器

    —顺序容器:vector,list,queue1.顺序容器的常见用法: #include <vector> #include <list> #include <queue ...

  4. C++ STL 顺序容器--list + 关联容器

    list 双向链表,可以双向遍历,既指向前驱节点,又指向后继但不能随机访问任意元素,可动态增加或者减少元素,内存管理自动完成,增加任何元素都不会使迭代器失效, 删除元素时,除了指向当前被删元素的迭代器 ...

  5. C++ Primer : 第十一章 : 关联容器之关联容器的迭代器和操作

    关联容器的操作 除了和顺序容器定义的类型之外,关联容器还定义了一下几种类型: 关联容器额外的类型别名  key_type    此容器类型的关键字类型 mapped_type  每个关键字关联的类型, ...

  6. iBinary C++STL模板库关联容器之map/multimap

    目录 一丶关联容器map/multimap 容器 二丶代码例子 1.map的三种插入数据的方法 3.map集合的遍历 4.验证map集合数据是否插入成功 5.map数据的查找 6.Map集合删除元素以 ...

  7. C++之容器(关联容器)

    关联容器和顺序容器的本质区别:关联容器是通过键存取和读取元素.顺序容器通过元素在容器中的位置顺序存储和访问元素.因此,关联容器不提供front.push_front.pop_front.back.pu ...

  8. Docker容器学习梳理 - 容器硬盘热扩容

    前面已介绍了docker很多知识点的操作记录,今天这里梳理下docker容器空间扩展的操作.默认情况下,物理机下创建的docker容器的空间是10G(虚拟机下创建的docker容器空间就是虚拟机的空间 ...

  9. Docker容器学习梳理 - 容器间网络通信设置(Pipework和Open vSwitch)

    自从Docker容器出现以来,容器的网络通信就一直是被关注的焦点,也是生产环境的迫切需求.容器的网络通信又可以分为两大方面:单主机容器上的相互通信,和跨主机的容器相互通信.下面将分别针对这两方面,对容 ...

  10. Docker容器学习梳理 - 容器时间跟宿主机时间同步

    在Docker容器创建好之后,可能会发现容器时间跟宿主机时间不一致,这就需要同步它们的时间,让容器时间跟宿主机时间保持一致.如下: 宿主机时间 [root@slave-1 ~]# date Fri M ...

随机推荐

  1. PCB MS SQL表值函数与CLR 表值函数 (例:字符串分割转表)

    将字符串分割为表表经常用到,这里 SQL表值函数与CLR  表值函数,两种实现方法例出来如下: SELECT * FROM FP_EMSDB_PUB.dbo.SqlSplit('/','1oz/1.5 ...

  2. PCB 周期日历

    在PCB行业一直以来没找到适合我们这行业的日历,主要存在2个差异导致. 1.周期差异:由于PCB 周期计算的复杂性,市面上无法找到符合PCB行业计算周期方式 (另一遍博文中有写周期计算逻辑) http ...

  3. java bean转Map

    /** * @author xxxxxxxxxxx * @param object * 待转化类 * @param format自定义转化类型 * @return Map<String,Stri ...

  4. 关于EasyUI datagrid editor combogrid搜索框的实现

    首先需要datagrid editor对combogrid的扩展,这个是别人实现的: $.extend($.fn.datagrid.defaults.editors, { combogrid: { i ...

  5. windows phone传感器

    Windows phone中的传感器主要包括加速计传感器.罗盘传感器.陀螺仪传感器等 加速计传感器 Accelerometer类是加速传感器的接口,Accelerometer类位于Windows.De ...

  6. PHP中的魔术方法和魔术常量

    看上去好像挺烦人,但只要通过例子测试一下,就明白了.不做测试,只是看,第二天还是不明白.当然我在抄其他人的日志,然后希望能是自己的理解就好,原文地址PHP的魔术方法和魔术敞亮简介和使用--LaraBo ...

  7. HDFS Shell命令操作与java代码操作

    (一)编程实现以下功能,并利用 Hadoop 提供的 Shell 命令完成相同任务: (1)     向 HDFS 中上传任意文本文件,如果指定的文件在 HDFS 中已经存在,则由用户来指定是追加到原 ...

  8. 修改Switch 的颜色

    1:效果图 2:布局 <Switch android:id="@+id/switch_bg" style="@style/switchStyle" and ...

  9. 启动Mysql时,提示error 2002 的解决办法

    故障描述 启动时提示ERROR 2002(HY000) 启动服务时,提示the server quit without updating PID file. 解决方法一: 1.由于mysql是卸载后重 ...

  10. 图方法:寻找无向图联通子集的JAVA版本

    图像处理中一般使用稠密方法,即对图像进行像素集合进行处理.在图像拓扑方面,更多地应用图计算方法. 寻找无向图联通子集的JAVA版本,代码: //查找无向图的所有连通子集//wishchin!!! pu ...