map 是一种关联容器,  提供一对一的关联, 关联的形式为: KEY----VALUE     关键字不重复。multimap与map类似,但是允许关键字重复

即:关键字和与之对应的值

关键字起到索引的作用, 在map中查找记录 就是根据关键字查找

关键字  和 值 可以是任意类型

map 也可看做是  关键字映射的集合, 即,map中不可出现重复的关键字,每条映射的关键字都是不同的。

map 是基于红黑树结构的,其查找时间为LOG(N)

如:

map<int, int >               //第一个为关键字,第二个为此关键字所对应的值     一个关键字只对应一个值, 是一对一的映射关系

map<CString,int>

map<int, CString>

map<CString,CString>

........

头文件

  1. #include <map>
  2. using namespace std;  //必须加上

1 插入元素

1)  insert函数插入

  1. map<int,int> idMap;
  2. idMap.insert(pair<int,int>(1,1));
  3. idMap.insert(map<int,int>::value_type(2,1));

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

判断是否插入成功

  1. map<int,int> idMap;
  2. idMap.insert(pair<int,int>(1,1));
  3. idMap.insert(map<int,int>::value_type(2,1));
  4. pair<map<int,int>::iterator,bool> InsertPair;
  5. InsertPair=idMap.insert (pair<int,int>(1,1));
  6. if (InsertPair.second==true)
  7. {
  8. cout<<"insert successfully";
  9. }else
  10. cout<<"insert failure";

  2 )数组插入方式

  1. map<int,int> idMap;
  2. idMap[1]=2;

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

但存在一个性能的问题。该方法会将每个插入值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。而用insert方法则可直接赋值为显示值。

2 判断是否存在

Returns the number of elements in a map whose key matches a parameter-specified key.

 
size_type count(
const Key&
_Key
) const

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.

  1. int num=idMap.count(1);
  2. if (num==0)
  3. {
  4. cout<<"the key 1 does not exist";
  5. }else{
  6. cout<<"exist";
  7. }

3 查找关键字

iterator find(
const Key&
_Key
);
const_iterator find(
const Key&
_Key
) const;
  1. map<int,int>::iterator it;
  2. it=idMap.find(2);
  3. if (it==idMap.end())
  4. {
  5. cout<<"can not find 2";
  6. }else{
  7. int first=it->first;
  8. int second=it->second;
  9. }

扩展:

 
iterator lower_bound(
const Key&
_Key
);
const_iterator lower_bound(
const Key&
_Key
) const;

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(
const Key&
_Key
);
const_iterator upper_bound(
const Key&
_Key
) const;

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 (
const Key&
_Key
) const;
pair <iterator, iterator> equal_range (
const Key&
_Key
);

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中不出现这个关键字

  1. p2 = m1.equal_range( 4 );
  2. // If no match is found for the key,
  3. // both elements of the pair return end( )
  4. if ( ( p2.first == m1.end( ) ) && ( p2.second == m1.end( ) ) )
  5. cout << "The map m1 doesn't have an element "
  6. << "with a key less than 40." << endl;
  7. else
  8. cout << "The element of map m1 with a key >= 40 is: "
  9. << p2.first -> first << "." << endl;

 

4 大小,包含多少个元素

Returns the number of elements in the map.

 
size_type size( ) const;
  1. int nSize=idMap.size();

5 遍历

前向迭代器

  1. map<int,int>::iterator it;
  2. for (it=idMap.begin ();it!=idMap.end();it++)
  3. {
  4. cout<<it->first<<endl;
  5. cout<<it->second<<endl;
  6. }

反向迭代器

  1. map<int,int>::reverse_iterator iter;
  2. for (iter=idMap.rbegin ();iter!=idMap.rend ();iter++)
  3. {
  4. cout<<iter->first<<endl;
  5. cout<<iter->second<<endl;
  6. }


6 删除

iterator erase(
iterator
_Where
);
iterator erase(
iterator
_First,
iterator
_Last
);
size_type erase(
const key_type&
_Key
);
  1. //迭代器删除
  2. map<int,int>::iterator it;
  3. it=idMap.find(1);
  4. idMap.erase(it);
  5. //关键字删除
  6. idMap.erase(1);
  7. //成片删除 或清空
  8. idMap.erase(idMap.begin (),idMap.end());
  9. //清空
  10. idMap.clear ();

7 基本函数

  1. C++ Maps是一种关联式容器,包含“关键字/值”对
  2. begin()          返回指向map头部的迭代器
  3. clear()         删除所有元素
  4. count()          返回指定元素出现的次数
  5. empty()          如果map为空则返回true
  6. end()            返回指向map末尾的迭代器
  7. equal_range()    返回特殊条目的迭代器对
  8. erase()          删除一个元素
  9. find()           查找一个元素
  10. get_allocator()  返回map的配置器
  11. insert()         插入元素
  12. key_comp()       返回比较元素key的函数
  13. lower_bound()    返回键值>=给定元素的第一个位置
  14. max_size()       返回可以容纳的最大元素个数
  15. rbegin()         返回一个指向map尾部的逆向迭代器
  16. rend()           返回一个指向map头部的逆向迭代器
  17. size()           返回map中元素的个数
  18. swap()            交换两个map
  19. upper_bound()     返回键值>给定元素的第一个位置
  20. value_comp()      返回比较元素value的函数

示例:

  1. map<int,vector<int>>  m_DianmingMap;
    1. AddDianmingMap(int nXueqi,int nXuehao)
    2. {
    3. //将此学生添加到已点名容器中
    4. map<int,vector<int>>::iterator it;
    5. it=m_DianmingMap.find(nXueqi);
    6. if (it==m_DianmingMap.end ()) //先查找关键字有无此学期ID
    7. {
    8. //容器中不存在 则添加
    9. vector<int>  int_Vec;
    10. int_Vec.push_back(nXuehao);
    11. m_DianmingMap[nXueqi]=int_Vec;
    12. }else{//在查找 此学期中 有无此学号ID
    13. vector<int>::iterator itVec=find(it->second.begin (),it->second.end(),m_nXuehaoID);
    14. if(itVec==it->second.end())
    15. {
    16. //没有此学生则添加
    17. it->second.push_back(nXuehao);
    18. }
    19. }
    20. return TRUE;
    21. }

map 用法的更多相关文章

  1. Collection List Set和Map用法与区别

    labels:Collection List Set和Map用法与区别 java 散列表 集合 Collection           接 口的接口      对 象的集合   ├   List   ...

  2. ES6中Set 和 Map用法

    JS中Set与Map用法 一.Set 1.基本用法 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. ...

  3. 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 ...

  4. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  5. C++:map用法及元素的默认值

    C++:map用法 一.map基本用法 键值对 第一个参数为键的类型,第二个参数为值的类型. 源代码 #include <iostream> #include <string> ...

  6. c++ STL map 用法

    map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...

  7. std::map用法

    STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...

  8. STL map 用法

    首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象  class pair可以将两个值视为一个单元.容器类别map和mul ...

  9. map用法详解

    转自:http://www.kuqin.com/cpluspluslib/20071231/3265.html Map是 STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在 ...

  10. UVA11995【I can guess the data structrue!!】【水】+UVA11991【map用法】

    先看UVA11995 两份代码一份直接用C写的,一份用STL写的 #include <iostream> #include <stdio.h> #include <str ...

随机推荐

  1. 使用 BenchmarkDotnet 测试代码性能 【Win10】单元测试中捕获异步方法的指定异常

    先来点题外话,清明节前把工作辞了(去 tm 的垃圾团队,各种拉帮结派.勾心斗角).这次找工作就得慢慢找了,不能急了,希望能找到个好团队,好岗位吧.顺便这段时间也算是比较闲,也能学习一下和填掉手上的坑. ...

  2. Windows命令实现匿名邮件发送

    在日常工具开发中,常常会有发送邮件的需求.在一些高级语言中,如Python.C#中,都有专门的邮件发送模块,如Python 中的 smtplib 模块.那么.一封邮件究竟是怎样发送到一个特定的邮箱呢? ...

  3. mac svn cornerstone 破解版资源以及使用方法(仅供学习,非商业使用)

    mac svn 可视化客户端,找了好久,不知道是我搜索的有问题还是怎么了,没有特别好用的. 后来发现了一个大神做的破解版的 cornerstone,具体大神的博客我给忘记了,后续找到会贴出地址,以供膜 ...

  4. 嵌入式开发之davinci--- 8127 和8148的区别

    1.主要的差别是8148有sata接口,8127没有 2.经过最近各方查找,应该是8107中把DSP砍掉了,8127如1楼所示 http://www.deyisupport.com/question_ ...

  5. 网络学习笔记:TCP/IP连网和Internet

    1.网关 由硬件和软件组成,实现不同网段间的数据传送. 常用路由器充当网关. 网关通常维护一份路由表,但只有少量的编址信息.它用这些信息把数据转发到知道更多信息的网关. 组成互联网骨干的网关称为核心网 ...

  6. document.body.className = document.body.className.replace("siteorigin-panels-before-js","");

    document.body.className = document.body.className.replace("siteorigin-panels-before-js",&q ...

  7. 容器HashSet原理(学习)

    一.概述 使用HashMap存储,非线程安全: 二.实现 HashSet 底层使用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,相关 HashSet 的操作,基本上都是直接调 ...

  8. Lexer and parser generators (ocamllex, ocamlyacc)

    Chapter 12 Lexer and parser generators (ocamllex, ocamlyacc) This chapter describes two program gene ...

  9. POJ 3294 UVA 11107 Life Forms 后缀数组

    相同的题目,输出格式有区别. 给定n个字符串,求最长的子串,使得它同时出现在一半以上的串中. 不熟悉后缀数组的童鞋建议先去看一看如何用后缀数组计算两个字符串的最长公共子串 Ural1517 这道题的思 ...

  10. bzoj2338

    计算几何 我们先把所有的线段求出来,我们发现只有两个线段等长且中点重合时才能构成矩形,那么线段有n*n条,我们按中点,长度排序,然后对于一条线段扫描所有符合条件的线段计算答案,这样看起来是O(n^3) ...