map 用法
map 是一种关联容器, 提供一对一的关联, 关联的形式为: KEY----VALUE 关键字不重复。multimap与map类似,但是允许关键字重复
即:关键字和与之对应的值
关键字起到索引的作用, 在map中查找记录 就是根据关键字查找
关键字 和 值 可以是任意类型
map 也可看做是 关键字映射的集合, 即,map中不可出现重复的关键字,每条映射的关键字都是不同的。
map 是基于红黑树结构的,其查找时间为LOG(N)
如:
map<int, int > //第一个为关键字,第二个为此关键字所对应的值 一个关键字只对应一个值, 是一对一的映射关系
map<CString,int>
map<int, CString>
map<CString,CString>
........
头文件
- #include <map>
- using namespace std; //必须加上
1 插入元素
1) insert函数插入
- map<int,int> idMap;
- idMap.insert(pair<int,int>(1,1));
- idMap.insert(map<int,int>::value_type(2,1));
用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的
判断是否插入成功
- map<int,int> idMap;
- idMap.insert(pair<int,int>(1,1));
- idMap.insert(map<int,int>::value_type(2,1));
- pair<map<int,int>::iterator,bool> InsertPair;
- InsertPair=idMap.insert (pair<int,int>(1,1));
- if (InsertPair.second==true)
- {
- cout<<"insert successfully";
- }else
- cout<<"insert failure";
2 )数组插入方式
- map<int,int> idMap;
- idMap[1]=2;
用数组方式就不同了,它可以覆盖以前该关键字对应的值
但存在一个性能的问题。该方法会将每个插入值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。而用insert方法则可直接赋值为显示值。
2 判断是否存在
Returns the number of elements in a map whose key matches a parameter-specified key.
size_type count( |
1 if the map contains an element whose sort key matches the parameter key; 0 if the map does not contain an element with a matching key.
- int num=idMap.count(1);
- if (num==0)
- {
- cout<<"the key 1 does not exist";
- }else{
- cout<<"exist";
- }
3 查找关键字
iterator find( |
- map<int,int>::iterator it;
- it=idMap.find(2);
- if (it==idMap.end())
- {
- cout<<"can not find 2";
- }else{
- int first=it->first;
- int second=it->second;
- }
扩展:
iterator lower_bound( |
Returns an iterator to the first element in a map with a key value that is equal to or greater than that of a specified key.
第一个等于或大于Key的元素
iterator upper_bound( |
Returns an iterator to the first element in a map that with a key having a value that is greater than that of a specified key.
第一个大于Key的元素
pair <const_iterator, const_iterator> equal_range ( |
A pair of iterators such that the first is the lower_bound of the key and the second is the upper_bound of the key.
Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字
- p2 = m1.equal_range( 4 );
- // If no match is found for the key,
- // both elements of the pair return end( )
- if ( ( p2.first == m1.end( ) ) && ( p2.second == m1.end( ) ) )
- cout << "The map m1 doesn't have an element "
- << "with a key less than 40." << endl;
- else
- cout << "The element of map m1 with a key >= 40 is: "
- << p2.first -> first << "." << endl;
4 大小,包含多少个元素
Returns the number of elements in the map.
size_type size( ) const; |
- int nSize=idMap.size();
5 遍历
前向迭代器
- map<int,int>::iterator it;
- for (it=idMap.begin ();it!=idMap.end();it++)
- {
- cout<<it->first<<endl;
- cout<<it->second<<endl;
- }
反向迭代器
- map<int,int>::reverse_iterator iter;
- for (iter=idMap.rbegin ();iter!=idMap.rend ();iter++)
- {
- cout<<iter->first<<endl;
- cout<<iter->second<<endl;
- }
6 删除
iterator erase( |
- //迭代器删除
- map<int,int>::iterator it;
- it=idMap.find(1);
- idMap.erase(it);
- //关键字删除
- idMap.erase(1);
- //成片删除 或清空
- idMap.erase(idMap.begin (),idMap.end());
- //清空
- idMap.clear ();
7 基本函数
- C++ Maps是一种关联式容器,包含“关键字/值”对
- begin() 返回指向map头部的迭代器
- clear() 删除所有元素
- count() 返回指定元素出现的次数
- empty() 如果map为空则返回true
- end() 返回指向map末尾的迭代器
- equal_range() 返回特殊条目的迭代器对
- erase() 删除一个元素
- find() 查找一个元素
- get_allocator() 返回map的配置器
- insert() 插入元素
- key_comp() 返回比较元素key的函数
- lower_bound() 返回键值>=给定元素的第一个位置
- max_size() 返回可以容纳的最大元素个数
- rbegin() 返回一个指向map尾部的逆向迭代器
- rend() 返回一个指向map头部的逆向迭代器
- size() 返回map中元素的个数
- swap() 交换两个map
- upper_bound() 返回键值>给定元素的第一个位置
- value_comp() 返回比较元素value的函数
示例:
- map<int,vector<int>> m_DianmingMap;
- AddDianmingMap(int nXueqi,int nXuehao)
- {
- //将此学生添加到已点名容器中
- map<int,vector<int>>::iterator it;
- it=m_DianmingMap.find(nXueqi);
- if (it==m_DianmingMap.end ()) //先查找关键字有无此学期ID
- {
- //容器中不存在 则添加
- vector<int> int_Vec;
- int_Vec.push_back(nXuehao);
- m_DianmingMap[nXueqi]=int_Vec;
- }else{//在查找 此学期中 有无此学号ID
- vector<int>::iterator itVec=find(it->second.begin (),it->second.end(),m_nXuehaoID);
- if(itVec==it->second.end())
- {
- //没有此学生则添加
- it->second.push_back(nXuehao);
- }
- }
- return TRUE;
- }
map 用法的更多相关文章
- Collection List Set和Map用法与区别
labels:Collection List Set和Map用法与区别 java 散列表 集合 Collection 接 口的接口 对 象的集合 ├ List ...
- ES6中Set 和 Map用法
JS中Set与Map用法 一.Set 1.基本用法 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. ...
- sort函数(cmp)、map用法---------------Tju_Oj_2312Help Me with the Game
这道题里主要学习了sort函数.sort的cmp函数写法.C++的map用法(其实和数组一样) Your task is to read a picture of a chessboard posit ...
- C++中的STL中map用法详解(转)
原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解 Map是STL的一个关联容器,它提供 ...
- C++:map用法及元素的默认值
C++:map用法 一.map基本用法 键值对 第一个参数为键的类型,第二个参数为值的类型. 源代码 #include <iostream> #include <string> ...
- c++ STL map 用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...
- std::map用法
STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用. 在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...
- STL map 用法
首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和mul ...
- map用法详解
转自:http://www.kuqin.com/cpluspluslib/20071231/3265.html Map是 STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在 ...
- UVA11995【I can guess the data structrue!!】【水】+UVA11991【map用法】
先看UVA11995 两份代码一份直接用C写的,一份用STL写的 #include <iostream> #include <stdio.h> #include <str ...
随机推荐
- 【树形dp小练】HDU1520 HDU2196 HDU1561 HDU3534
[树形dp]就是在树上做的一些dp之类的递推,由于一般须要递归处理.因此平庸情况的处理可能须要理清思路.昨晚開始切了4题,作为入门训练.题目都很easy.可是似乎做起来都还口以- hdu1520 An ...
- [NOI2015Day1]解题报告
今天一起做NOI的题. 我仅仅想说SunshinAK了好神啊. T3数据好坑啊,打表竟然被编译环境卡掉了... T1:程序自己主动分析 (http://www.lydsy.com/JudgeOnlin ...
- nRF52832之硬件I2C
这几天一直在折腾nRF52832的硬件I2C,到了今天最终出现了成果,在此也印证了那句话:"耕耘就有收获" 52832的硬件I2C尽管官方提供了demo,可是自己对I2C通信理解的 ...
- ViewControl的size设为freeform
freeform的用处是让你写一些不标准的view,比如说自定义一个cell,或者自己写一个小的VIEW,freeform的XIB是可以自己拖拽更改大小的
- 6 Workbook 对象
6.1 在奔跑之前先学会走路:打开和关闭工作薄 代码清单6.1:一个完整的工作薄批处理框架 '代码清单6.1:一个完整的工作薄批处理框架 Sub ProcessFileBatch() Dim nInd ...
- 基于JWT机制的单点登录
使用JWT实现单点登录时,需要注意token时效性.token是保存在客户端的令牌数据,如果永久有效,则有被劫持的可能.token在设计的时候,可以考虑一次性有效或一段时间内有效.如果设置有效时长,则 ...
- [SDOI2011]消防(单调队列,树的直径,双指针)
消防 2011年 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 某个国家有n个城市,这n个城市中任意两个都连通且有 ...
- BADI FCODE(菜单) 增强
菜单增强功能只能用于非依赖于过滤器的一次性BADI(不是多用途的). 目前,菜单增强功能只能与程序增强功能(界面)一起创建. 定义一个没有过滤器的一次性增强 2.Classic Badi在FCODE ...
- (数论 欧拉筛法)51NOD 1106 质数检测
给出N个正整数,检测每个数是否为质数.如果是,输出"Yes",否则输出"No". Input 第1行:一个数N,表示正整数的数量.(1 <= N &l ...
- Parameterized testing with any Python test framework
1. 在进行单元测试时,很多时候需要进行参数化 尝试过使用 from nose_parameterized import parameterized 但在使用过程中会报错,后来将上面的内容改为了下面的 ...