美文网首页
STL之map详解

STL之map详解

作者: 藝龍 | 来源:发表于2017-10-31 10:25 被阅读0次

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。

    1. map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。
    2. map的功能
      自动建立Key - value的对应。key 和 value可以是任意你需要的类型。
      根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
      快速插入Key -Value 记录。
      快速删除记录
      根据Key 修改value记录。
      遍历所有记录。
    3. 使用map
      使用map得包含map类所在的头文件
      #include <map> //注意,STL头文件没有扩展名.h
      map对象是模板类,需要关键字和存储对象两个模板参数:
      std:map<int,string> personnel;
      这样就定义了一个用int作为索引,并拥有相关联的指向string的指针.
      为了使用方便,可以对模板类进行一下类型定义,
      typedef map<int,CString> UDT_MAP_INT_CSTRING;
      UDT_MAP_INT_CSTRING enumMap;
    4. map应用实例
    #include <map>  
    #include <string>  
    #include <iostream>
    #include <fstream>
    #include <iterator>
    using namespace std;  
    
    //map three method
    map<int,string> mapstr;
    
    void inA()
    {
        mapstr.clear();
        mapstr.insert(pair<int,string>(1,"student_one"));
        mapstr.insert(pair<int,string>(2,"student_two"));
        mapstr.insert(pair<int,string>(3,"student_three"));
    }
    
    void inB()
    {
        mapstr.clear();
        mapstr.insert(map<int,string>::value_type(1,"student_one"));
        mapstr.insert(map<int,string>::value_type(2,"student_two"));
        pair<map<int,string>::iterator, bool> rt = mapstr.insert(map<int,string>::value_type(3,"student_three"));
        if (rt.second == true)
        {
            cout << "INSERT SUCCESSFUL" <<endl;
        }
        else
        {
            cout << "INSERT FAIL" <<endl;
        }
    
    }
    
    void inC()
    {
        mapstr.clear();
        mapstr[1] = "student_one";
        mapstr[2] = "student_two";
        mapstr[3] = "student_three";
    }
    
    void outA()
    {
        map<int, string>::iterator iter;  
        for (iter = mapstr.begin(); iter != mapstr.end(); iter++)
        {
            cout << iter->first << "---" <<iter->second<<endl;
        }
    }
    
    void outB()
    {
        map<int, string>::reverse_iterator iter;  
        for(iter = mapstr.rbegin(); iter != mapstr.rend(); iter++)  
        {
            cout<<iter->first<<"  "<<iter->second<<endl;  
        }
    }
    
    void outC()
    {
        int nSize = mapstr.size();  
        for(int i = 1;i<=nSize;i++)
        {
            cout << mapstr[i] <<endl;
        }
    }
    
    void searchA()
    {
        map<int,string>::iterator iter = mapstr.find(1);
        if (iter != mapstr.end())
        {
            cout << iter->second <<endl;
        }
    }
    
    
    void delA()
    {
        map<int, string>::iterator iter = mapstr.find(1);
        map<int, string>::iterator  a = mapstr.erase(iter);
    }
    
    void delB()
    {
        int b = mapstr.erase(2);
    }
    

    相关文章

      网友评论

          本文标题:STL之map详解

          本文链接:https://www.haomeiwen.com/subject/nrgvpxtx.html