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

1 if the map contains an element whose sort ke
y 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(
   const Key& _Key
);
const_iterator find(
   const Key& _Key
) const;

 

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

 

    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(
   iterator _Where
);
iterator erase(
   iterator _First,
   iterator _Last
);
size_type erase(
   const key_type& _Key
);

 

    //迭代器删除  
    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使用.xml的更多相关文章

  1. Json、JavaBean、Map、XML之间的互转

    思路是JavaBean.Map.XML都可以用工具类很简单的转换为Json,进而实现互相转换 1.Map.XML与Json互转 mvn依赖 <dependency> <groupId ...

  2. xml转Map,对象,Map转xml,inputs tram 转xml 字符串的工具类方法

    众所周知,大家在微信开发工程中,由于微信开发文档中,对于消息的接收发送都是基础xml数据的(太坑了),所以我们需要对XML进行解析转换: 1.我们先引入所需要的依赖 dom4j (解析xml的),xs ...

  3. map 转换 xml ; xml转map

    public class MessageKit { public static String map2xml(Map<String, String> map) throws IOExcep ...

  4. 01java中常用的一些算法工具——map转xml和xml转map

    借鉴博客:https://blog.csdn.net/Goodbye_Youth/article/details/80937862 他这篇写的不错,多层嵌套的map也能转成xml 这篇也不错:http ...

  5. JSON,Bean,XML,List,Map

    http://blog.csdn.net/superit401/article/details/51728929 JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把J ...

  6. Ibatis学习总结3--SQL Map XML 映射文件

    在前面的例子中,只使用了 SQL Map 最简单的形式.SQL Map 的结构中还有其他更多 的选项.这里是一个 mapped statement 较复杂的例子,使用了更多的特性. <sqlMa ...

  7. Ibatis学习总结2--SQL Map XML 配置文件

    SQL Map 使用 XML 配置文件统一配置不同的属性,包括 DataSource 的详细配置信息, SQL Map 和其他可选属性,如线程管理等.以下是 SQL Map 配置文件的一个例子: Sq ...

  8. XML转换为Map通用算法实现 Java版本(Stax实现)

    目前项目中需要将XML转换为Map,下面给出了自己的代码实现. 后续将为大家提供Dom版本的实现. 请各路大神给予各种优良实现. 场景: 在项目中需要解析XML文本字符串,需要将XML文本字符串映射为 ...

  9. mybatis mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map

    用mybaits 写一个关联查询 查询商品表关联商品规格表,并查询规格表中的数量.价格等,为了sql重用性,利用 association 节点 查询 结果并赋值报错 商品表的mapper文件为Gooo ...

随机推荐

  1. Linux命令-mkdir

    mkdir用于创建空白文件夹 参数-p用于连续创建多层目录 参数-m用于创建自定义的目录权限 [root@localhost test]# mkdir a/b/c/d mkdir: 无法创建目录&qu ...

  2. awk案例学习

    awk是一个强大的文本分析工具,awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理.awk语言的最基本功能是在文件或者字符串中基于指定规则浏览和抽取信息,awk抽取 ...

  3. Alpha、 Beta、build、release测试

    alpha(内测) alpha测试是由一个用户在开发环境下进行的测试,也可以是公司内部的用户在模拟实际操作环境下进行的测试.alpha测试的目的是评价软件产品的FLURPS(即功能.局域化.可使用性. ...

  4. android 四种堆状态

    总结下: ====> 建议首先阅读下面两篇文章,这样才可以更好的理解Activity的加载模式: Android的进程,线程模型 http://www.cnblogs.com/ghj1976/a ...

  5. EasyUi datagrid 表格分页例子

    1.首先引入 easyui的 css 和 js 文件 2.前台 需要写的js //源数据 function Async(action,args,callback){  $.ajax({  url: a ...

  6. JVM的相关知识整理和学习--(转载)

    JVM是虚拟机,也是一种规范,他遵循着冯·诺依曼体系结构的设计原理.冯·诺依曼体系结构中,指出计算机处理的数据和指令都是二进制数,采用存储程序方式不加区分的存储在同一个存储器里,并且顺序执行,指令由操 ...

  7. 利用SOLR搭建企业搜索平台 之——solr的查询语法

      1. 首先假设我的数据里fields有:name, tel, address 预设的搜寻是name这个字段, 如果要搜寻的数据刚好就是 name 这个字段,就不需要指定搜寻字段名称. 2. 查询规 ...

  8. 适用于Firemonkey的Json解析对象XsuperObject使用方法介绍

    XSuperObject是适用于FileMonkey平台的JSON解析组件,能够在Android,IOS,MACOS,WINDOWS等多个平台使用 点击下载 Sample JSON { "n ...

  9. UVa 11100 The Trip, 2007

    今天的教训:做题要用大块的时间来做,上午做一下,做题做到一半就去忙别的事,那么后面再做的时候就无限CE,WA了.因为你很难或者需要很长时间来找回当时的思路. 题意:就像套瓷娃娃一样,有n个包,大小可能 ...

  10. UVa 455 Periodic Strings

    题意:给出一个字符串,找出它的最小的周期,枚举从1到len的周期,看是否满足. #include<iostream> #include<cstdio> #include< ...