map的特性是,所有元素都会根据元素的键值自动被排序。map的所有元素都是pair,同时拥有键值(key)和实值(value)。pair的第一元素被视为键值,第二元素被视为实值。map不允许两个元素拥有相同的键值
multimap的特性以及用法与map完全相同,唯一的差别在于它允许键值重复。unordered_map与map的区别就在于不会根据key的大小进行排序.

1.插入数据的方法

 #include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> Map1,Map2;
//insert函数插入数据
Map1.insert(pair<int, string>(, "A"));
Map1.insert(pair<int, string>(, "B"));
Map1.insert(pair<int, string>(, "C"));
map<int, string>::iterator iter;
cout<<"Map1:"<<endl;
for(iter = Map1.begin(); iter != Map1.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<" ";
} //数组方式插入数据
Map2[] = "A";
Map2[] = "B";
Map2[] = "C";
cout<<endl<<"Map2:"<<endl;
for(iter = Map2.begin(); iter != Map2.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<<" ";
}
return ;
}

使用insert函数和使用数组插入数据的区别就在于:

用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作

是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值。

2.数据的查找(包括判定这个关键字是否在map中出现)

(1)若要实现判断一个key是否存在,如果存在就输出,不存在就不输出的功能,则可以使用count函数。count函数的功能是统计关键字出现的次数。map对于关键字来说是唯一的,也就是说在map中不存在等价的两个(以上)元素,因此某个元素在map/set中出现的次数最多只能为1,用count得到的结果不是0就是1。

(2)用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,程序说明:

 #include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> Map;
//insert函数插入数据
Map.insert(pair<int, string>(, "A"));
Map.insert(pair<int, string>(, "B"));
Map.insert(pair<int, string>(, "C"));
map<int, string>::iterator iter;
//返回key值为1的迭代器位置
iter = Map.find();
if(iter != Map.end())
cout<<"Find,the value is "<<iter->second<<endl;
else
cout<<"Not find"<<endl;
return ;
}

3.数据的清空与判空
清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map
4. 数据的删除
这里要用到erase函数,它有三个重载了的函数,下面在例子中详细说明它们的用法

 #include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> Map;
//insert函数插入数据
Map.insert(pair<int, string>(, "A"));
Map.insert(pair<int, string>(, "B"));
Map.insert(pair<int, string>(, "C"));
map<int, string>::iterator iter; //用迭代器删除key为2的数据
iter = Map.find();
Map.erase(iter);
//打印输出
for(iter = Map.begin(); iter != Map.end(); iter ++)
{
cout<<iter->first<<" "<<iter->second<<" ";
}
cout<<endl; Map.insert(pair<int, string>(, "B")); //用关键字删除key为2的数据
int n = Map.erase();//删除成功返回1,否则返回0
if(n == )
{
//打印输出
for(iter = Map.begin(); iter != Map.end(); iter ++)
{
cout<<iter->first<<" "<<iter->second<<" ";
}
}
cout<<endl; Map.insert(pair<int, string>(, "B")); //用迭代器,成片的删除
//把整个map清空
Map.erase(Map.begin(),Map.end());
for(iter = Map.begin(); iter != Map.end(); iter ++)
{
cout<<iter->first<<" "<<iter->second<<" ";
}
return ;
}

[STL] map,multimap,unordered_map基本用法的更多相关文章

  1. C++中map和unordered_map的用法

    1. 简介 map和unordered_map都是c++中可以充当字典(key-value)来用的数据类型,但是其基本实现是不一样的. 2. map 对于map的底层原理,是通过红黑树(一种非严格意义 ...

  2. 2.9 C++STL map/multimap容器详解

    文章目录 2.9.1 引入 2.9.2 代码示例 map案列 multimap案列 2.9.3 代码运行结果 总结 2.9.1 引入 map相对于set区别,map具有键值和实值,所有元素根据键值自动 ...

  3. 关于c++ STL map 和 unordered_map 的效率的对比测试

    本文采用在随机读取和插入的情况下测试map和unordered_map的效率 笔者的电脑是台渣机,现给出配置信息 处理器 : Intel Pentium(R) CPU G850 @ 2.90GHz × ...

  4. STL::map/multimap

    map: 默认根据 key 排序(从小到大),能够通过 backet operator(operator [ ]) 来获取元素,内部由二叉搜索树来实现(binary search trees). mu ...

  5. C++使用: C++中map的基本操作和用法

    在阅读SSD代码中发现作者使用了C++中的map方法,因此搜索该关联式容器的使用方法,在这里一并总结. 一.Map 簡介 Map是STL的一個容器,它提供一對一的hash. 第一個可以稱為關鍵字(ke ...

  6. STL——map/unordered_map基础用法

    map /multimap map是STL里重要容器之一. 它的特性总结来讲就是:所有元素都会根据元素的键值key自动排序(也可根据自定义的仿函数进行自定义排序),其中的每个元素都是<key,  ...

  7. STL:map/multimap用法详解

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

  8. STL之六:map/multimap用法详解

    转载于:http://blog.csdn.net/longshengguoji/article/details/8547007 map/multimap 使用map/multimap之前要加入头文件# ...

  9. (转载)STL map与Boost unordered_map的比较

    原链接:传送门 今天看到 boost::unordered_map,它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合 ...

  10. STL Map和multimap 容器

    STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力.       ...

随机推荐

  1. gmtime与mktime的重新编写

    这几日发现Linux的mktime与gmtime所处理的数据类型是32位的,即代表时间设置只能设置到2038年,在公司的产品要实现这个时间的突破还是得自己写一个新的处理时间的函数. 作为一个刚毕业的程 ...

  2. 慎用静态类static class

    偶然翻到一篇有趣的帖子: class - When to Use Static Classes in C# - Stack Overflowhttp://stackoverflow.com/quest ...

  3. 成都Uber优步司机奖励政策(1月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. Hihocoder #1515 : 分数调查

    #1515 : 分数调查 http://hihocoder.com/problemset/problem/1515 分析 带权并查集. 如果把每个人抽象成一个点,之间的关系抽象成边.那么如果询问的两个 ...

  5. Ubuntu Server 下将HTML页面转换为PNG图片

    零.前言 最近做一个网站,需要将网页转换为图片.由于服务器是Ubuntu Server,没有图形界面,所以实现的过程中遇到了很多问题.记录下来备用. 一.安装CutyCapt CutyCapt是一个可 ...

  6. 程序员的冷笑话 python版本

    在伯乐在线上看到了个冷笑话,感觉很有意思. void tellStory() { printf("从前有座山\n"); printf("山上有座庙\n"); p ...

  7. 「题目代码」P1049~P1053(Java)

    P1049 谭浩强C语言(第三版)习题6.5 import java.util.*; import java.io.*; import java.math.BigInteger; import jav ...

  8. AirtestIDE实践二:Poco框架试用

    上一篇用airtest框架做了一个梦幻西游手游的DEMO,这次看看poco的强大之处.首先安装poco:pip install pocoui 其次,把SDK集成到你家游戏中,我这直接用官网提供的一个U ...

  9. Java开发工程师(Web方向) - 04.Spring框架 - 第3章.AOP技术

    第3章--AOP技术 Spring框架 - AOP概述 笔记https://my.oschina.net/hava/blog/758873Spring框架 - AOP使用 笔记https://my.o ...

  10. [递推+矩阵快速幂]Codeforces 1117D - Magic Gems

    传送门:Educational Codeforces Round 60 – D   题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...