zoukankan      html  css  js  c++  java
  • STL之map

    Map概述

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

    一、map的说明

    1.头文件

    #include<map>

    2.定义

    map<string, int> my_Map;
    或者是typedef map<string, int> MY_MAP;
    MY_MAP my_Map;

    3.插入数据

    (1) my_Map[“a”] = 1;
    (2) my_Map.insert(map<string, int>::value_type(“b”,2));
    (3) my_Map.insert(pair<string,int>(“c”,3));
    (4) my_Map.insert(make_pair<string,int>(“d”,4));

    4.查找数据和修改数据

    (1) int i = my_Map[“a”];
    my_Map[“a”] = i;
    (2) MY_MAP::iterator my_Itr;
    my_Itr.find(“b”);
    int j = my_Itr->second;
    my_Itr->second = j;
    不过注意,键本身是不能被修改的,除非删除。

    5.删除数据

    (1) my_Map.erase(my_Itr);
    (2) my_Map.erase(“c”);
    还是注意,第一种情况在迭代期间是不能被删除的,道理和foreach时不能删除元素一样。

    6.迭代数据

    for (my_Itr=my_Map.begin(); my_Itr!=my_Map.end(); ++my_Itr) {}

    7.其它方法

    my_Map.size() 返回元素数目
    my_Map.empty() 判断是否为空
    my_Map.clear() 清空所有元素

    二、几点注意事项

    1.关于插入操作

    (1) my_Map[“a”] = 1;
    (2) my_Map.insert(map

    2. 关于查找

    (1)用count函数来判定关键字是否出现,了count函数的返回值只有两个,要么是0,要么是1,查找到返回1。
    (2)用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器.

    3.关于删除

    一下代码把整个map清空
    mapStudent.earse(mapStudent.begin(), mapStudent.end());
    成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合

    三、实例代码

    1.study1

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <map>
    /*
        目标:熟悉一下map的基础用法
              涉及到插入操作、和迭代器的使用
    */
    using namespace std;
    
    int main()
    {
        map<int, string> mapStudent;
        mapStudent.insert(pair<int, string>(1, "student_one"));
        mapStudent.insert(pair<int, string>(2, "student_two"));
        mapStudent.insert(pair<int, string>(3, "student_three"));
        map<int, string>::iterator iter;
        for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){
           cout<<iter->first<<"   "<<iter->second<<endl;
        }
        return 0;
    }
    

    2.study2

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <map>
    /*
        目标:明确第一个位置的数据永远为关键词,在执行插入操作时一直保持关键词有序
    */
    using namespace std;
    
    int main()
    {
        map<int, string> mapStudent;
        mapStudent.insert(pair<int, string>(1, "student_one"));
        mapStudent.insert(pair<int, string>(2, "student_two"));
        mapStudent.insert(pair<int, string>(3, "student_three"));
        mapStudent[4] = "TEST";
        map<int, string>::iterator iter;
        for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){
           cout<<iter->first<<"   "<<iter->second<<endl;
        }
        return 0;
    }
    

    3.study3

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <map>
    /*
        学习目标:
        区分insert和数组式的插入操作的区别
    */
    using namespace std;
    
    int main()
    {
        typedef map<string, int> mymap;
        mymap a;
        //数组式插入操作
        a["A"]=0;
        a["C"]=2;
        a["B"]=1;
        mymap::iterator iter;
        for(iter = a.begin();iter!= a.end();iter++){
            cout<<iter->first<<"	"<<iter->second<<endl;
        }
        printf("
    ");
        a["A"]=1;
        for(iter = a.begin();iter!= a.end();iter++){
            cout<<iter->first<<"	"<<iter->second<<endl;
        }
        printf("
    ");
        //可以发现这里A的数据被修改为1
        //再使用insert操作试试
        a.insert(pair<string,int>("A",0));
        for(iter = a.begin();iter!= a.end();iter++){
            cout<<iter->first<<"	"<<iter->second<<endl;
        }
        //可以发现数据没有发生改变,可以说明插入操作失败
    
    
        return 0;
    }
    

    4.study4

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <map>
    /*
        学习目标:
        获得当前map容器的大小
    */
    using namespace std;
    
    int main()
    {
        ios::sync_with_stdio(false);
        typedef map<string, int> mymap;
        mymap a;
    
        a["A"]=0;
        a["C"]=2;
        a["B"]=1;
        mymap::iterator iter;
        for(iter = a.begin();iter!= a.end();iter++){
            cout<<iter->first<<"	"<<iter->second<<endl;
        }
        //printf("
    ");
        cout<<a.size()<<endl;
        return 0;
    }
    

    5.study5

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <algorithm>
    #include <map>
    /*
        学习目标:
        map数据的查找(只能查找first)
        count只能查找是否有,返回值为0或1
        find能查找出是否有,返回值为迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器
        意外发现:
        a[first]++;代表first对应的second自增1,前提是second类型是int
    */
    using namespace std;
    
    int main()
    {
        ios::sync_with_stdio(false);
        typedef map<string, int> mymap;
        mymap a;
        a["A"]=0;
        a["B"]=0;
        a["C"]=2;
        a["A"]++;//自增运算
        a["A"]++;
        a["A"]++;
        mymap::iterator iter;
        for(iter = a.begin();iter!= a.end();iter++){
            cout<<iter->first<<"	"<<iter->second<<endl;
        }
        int ret = a.count("A");
        mymap::iterator iter1 = a.find("D");
        if(iter1 == a.end()){
            cout<<"NO FIND"<<endl;
        }else{
            cout<<iter1->second<<endl;
        }
    
        return 0;
    }
    
    

    6.study6

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <algorithm>
    #include <map>
    /*
        学习目标:
        map数据的清空clearm,判断是否为空可用empty
    
    */
    using namespace std;
    
    int main()
    {
        ios::sync_with_stdio(false);
        typedef map<string, int> mymap;
        mymap a;
        a["A"]=0;
        a["B"]=0;
        a["C"]=2;
        a["A"]++;//自增运算
        a["A"]++;
        a["A"]++;
        mymap::iterator iter;
        for(iter = a.begin();iter!= a.end();iter++){
            cout<<iter->first<<"	"<<iter->second<<endl;
        }
        a.clear();
        iter = a.begin();
        if(a.begin() == a.end()){
            cout<<"EMPTY"<<endl;
        }
    
        if(a.empty()){
            cout<<"EMPTY"<<endl;
        }
    
        return 0;
    }
    

    7.study7

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <algorithm>
    #include <map>
    /*
        学习目标:
        map数据的删除erase
    
    */
    using namespace std;
    
    int main()
    {
        ios::sync_with_stdio(false);
        typedef map<string, int> mymap;
        mymap a;
        a["A"]=0;
        a["B"]=0;
        a["C"]=2;
        a["A"]++;//自增运算
        a["A"]++;
        a["A"]++;
        mymap::iterator iter;
        for(iter = a.begin();iter!= a.end();iter++){
            cout<<iter->first<<"	"<<iter->second<<endl;
        }
        cout<<endl;
        a.erase("B");
        for(iter = a.begin();iter!= a.end();iter++){
            cout<<iter->first<<"	"<<iter->second<<endl;
        }
    
        return 0;
    }
    

    四、深度拓展

    C++ STL map 用法
    STL–迭代器(iterator)使用详解
    C++学习之Pair

    五、几道水题练练手

    HDOJ.1004
    HDOJ.1029
    HDOJ.1075
    HDOJ.1263
    HDOJ.2072
    HDOJ.2094

    六、水题题解

    (下面题解中有题目连接)

    HDOJ.1029

    HDOJ.1075

    HDOJ.1263

    HDOJ.2072

    HDOJ.2094

  • 相关阅读:
    String类的操作方法
    操作日期时间类 Calendar类
    JAVA中Date类的使用
    集合方法整理
    JAVA日期——java.util.date类的操作
    关于Collections的操作方法
    java中集合
    java中继承的关系
    java
    封装继承多态-java面向对象三大特征
  • 原文地址:https://www.cnblogs.com/pengwill/p/7367233.html
Copyright © 2011-2022 走看看