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. Ajax的学习笔记(一)

    AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),ajax并不是一门单独的语言,而是一种技术,是指一种创建交互式网页应用的网页开发技术. ...

  2. CUDA中多维数组以及多维纹理内存的使用

    纹理存储器(texture memory)是一种只读存储器,由GPU用于纹理渲染的图形专用单元发展而来,因此也提供了一些特殊功能.纹理存储器中的数据位于显存,但可以通过纹理缓存加速读取.在纹理存储器中 ...

  3. C# break语句

    一.C# break语句 break语句用于终止它后面的所有循环语句,使控制流程跳转到break语句所在层的外面,以便结束本层的所有循环.如果有多个循环语句进行嵌套,break语句则会跳到它所在层的外 ...

  4. React后台管理系统-品类的增加、修改和查看

    1.页面 2.品类列表展示 let listBody = this.state.list.map((category, index) => {             return (      ...

  5. MySQL常见错误分析与解决方法总结

    MySQL常见错误分析与解决方法总结 一.Can't connect to MySQL server on 'localhost' (10061)翻译:不能连接到 localhost 上的mysql分 ...

  6. html编写头部,mata的含义

    <meta name="viewport" content="width=device-width, initial-scale=1.0"> con ...

  7. java高并发之CountDownLatch,CyclicBarrier和join

    晚上打车回家,在车上看到一篇文章<22岁大学生获谷歌天价Offer,年薪千万!>,讲的是印度一个22岁大学生多次参加ACM大赛,开源多个项目,以非常牛逼的履历通过了谷歌的AI测试,斩获谷歌 ...

  8. 连接MYSQL 错误代码2003

    问题是服务里面mysql没有启动或者mysql服务丢失 解决办法: 开始->运行->cmd,进到mysql安装的bin目录(以我的为例,我的安装在D盘)D:\MySQL\bin>my ...

  9. sql server几种Join的区别测试方法与union表的合并

    /* sql server几种Join的区别测试方法 主要来介绍下Inner Join , Full Out Join , Cross Join , Left Join , Right Join的区别 ...

  10. php红包算法函数[优化]

    php红包算法 <?php header("Content-Type: text/html;charset=utf-8");//输出不乱码,你懂的 $total=10000; ...