map

1.insert

第一种:用insert函数插入pair数据
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> map;
map.insert(pair<int, string>(, “one”));
map.insert(pair<int, string>(, “two”));
map.insert(pair<int, string>(, “three”));
map<int, string>::iterator iter;
for(iter = map.begin(); iter != map.end(); iter++)
{
cout<<iter->first<<” ”<<iter->second<<end;
}
}
第二种:用insert函数插入value_type数据
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> map;
map.insert(map<int, string>::value_type (, “one”));
map.insert(map<int, string>::value_type (, “two”));
map.insert(map<int, string>::value_type (, “three”));
map<int, string>::iterator iter;
for(iter = map.begin(); iter != map.end(); iter++)
{
cout<<iter->first<<” ”<<iter->second<<end;
}
}
第三种:用数组方式插入数据
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> map;
map[]=“one”;
map[]=“two”;
map[]= “three”;
map<int, string>::iterator iter;
for(iter = map.begin(); iter != map.end(); iter++)
{
cout<<iter->first<<” ”<<iter->second<<end;
}
}

map 总是以(key,value)的形式存在,当插入的数据的key已经存在时,会形成覆盖,map内部使用红黑树实现。

2.size
返回map的大小
 
3.遍历
  可以使用迭代器方式,如1中的例子
  也可以使用[],但是注意索引从1开始
 

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> map;
map.insert(pair<int, string>(, “one”));
map.insert(pair<int, string>(, “two”));
map.insert(pair<int, string>(, “three”));
for(int index=;index<=map.size();index++)
{
cout<<map[index]<<endl;
}
}

4.查找

map.find()如果查找到,返回指向结果的迭代器,如果没有找到到,返回map.end()

5.上下界

lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器),返回键值>=给定元素的第一个位置
upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器),返回键值>给定元素的第一个位置

6.清空与判空

清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map
 
multimap和map的区别在于,multimap允许key重复,其他没啥特别的地方,底层也是红黑树实现

STL之map&multimap使用简介的更多相关文章

  1. STL:map/multimap用法详解

    map/multimap 使用map/multimap之前要加入头文件#include<map>,map和multimap将key/value当作元素,进行管理.它们可根据key的排序准则 ...

  2. 【STL】-Map/Multimap的用法

    初始化: map<string,double> salaries; 算法: 1. 赋值.salaries[ "Pat" ] = 75000.00; 2. 无效的索引将自 ...

  3. STL——容器(Map & multimap)的简述与构造

    1. map/multimap 的简介 map 是标准的关联式容器,一个 map 里存储的元素是一个键值对序列,叫做 (key,value) 键值对.它提供基于 key 快速检索数据的能力. map ...

  4. 09--STL关联容器(map/multimap)

    一:map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...

  5. 【C++ STL】Map和Multimap

    1.结构 Map和multimap将key/value pair(键值/实值 队组)当作元素,进行管理.他们根据key的排序准则将元素排序.multimap允许重复元素,map不允许. 元素要求: k ...

  6. STL中的map/multimap小结

    (1)使用map/multimap之前必须包含头文件<map>:#include<map> 并且和所有的关联式容器一样,map/multimap通常以平衡二叉树来完成 (2)n ...

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

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

  8. STL之Map和multimap容器

    1.Map和multimap容器 1)map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. 2)map中key值是唯一的.集合中的元素按一 ...

  9. STL中 map 和 multimap

    1. 所在头文件<map>. 命名空间std, 声明如下: namespace std{ template <class Key,class T, class Compare = l ...

随机推荐

  1. apache配置局域网访问

    1.配置vhost.conf NameVirtualHost 192.168.2.74:80 <VirtualHost 192.168.2.74:80> DocumentRoot /var ...

  2. Linux笔记(开机自动将kerne log保存到SD卡中)

    有时候为了测试机器的稳定性,需要煲机测试几天的情况,这个时候机器已经封装好,不能再接串口线出来. 为了追溯问题,就需要将log信息保存下来. 于是就需要这样一个功能:系统启动后,自动将kernel的l ...

  3. 开源项目托管github步骤

    一.在github新建项目,复制到本地更改之后命令提交. 1.进入github主页新建项目:https://github.com/ccyinghua 2.复制项目地址 3.打开git Bash 命令行 ...

  4. 写给iOS小白的MVVM教程(序)

    这几天,需要重构下部分代码,这里简要记录下.但是涉及的技术要点还是很多,所以分为多个篇章叙述.此教程来源于,并将于应用于实践,不做过多的概念性阐释和争论.每个篇章都会附上实际的可执行的代码.因涉及的技 ...

  5. linux环境下nginx配置

    1.反向代理配置 #  nginx/conf/nginx.conf

  6. 想学习一下node.js,重新安装配置了node

    根据这个网站上的教程安装配置的,还不错一次就成功了.觉得安装没什么,就是配置路径的时候容易错. http://www.runoob.com/nodejs/nodejs-install-setup.ht ...

  7. py2exe安装使用

    一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你就可以不用装python而在windows系统上运行这个可执行程序. py2e ...

  8. python__系统 : socket_TCP相关

    tcp和udp对比起来.还是tcp相对稳定一些,但是因为有三次挥手和四次握手,以及确认包(ack)的存在,可能在速度上会比udp慢. 用python的socket模块可以建立tcp服务端: from ...

  9. PHP生成特定长度的纯字母字符串

    PHP中,md5().uniqid()函数可以返回32位和13位不重复的字符串,但是这些字符串都可能包含有数字.如果需要纯字母的字符串,而且长度不定,比如8位,那么直接用这两个函数无法达到效果. 这时 ...

  10. 插入排序算法Java实现

    一. 算法描述 插入即表示将一个新的数据插入到一个有序数组中,并继续保持有序.例如有一个长度为N的无序数组,进行N-1次的插入即能完成排序:第一次,数组第1个数认为是有序的数组,将数组第二个元素插入仅 ...