zoukankan      html  css  js  c++  java
  • STL map 详细用法

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个称为该关键字的值)的数据 处理能力。

    需要的库

    #include <map>

    基本操作

    定义

    map<string,int>m;

    这是定义了一个以string为关键字,以int为值的map

    插入

    方法1:

    map<string,int>m;
    m["Bob"]=101;    
    m["Alice"]=102;  
    m["Eric"]=103;   

    方法2:

    m.insert(pair<string,int>("Lee",104));

    方法3:

    m.insert(map<string,int>::value_type("Karen",105)); 

    遍历

    定义一个迭代指针iter,使其指向map,实现对map的遍历。

    #include<bits/stdc++.h>
    
    using namespace std;
    
    int main()
    {
        map<string,int>m;
        m["Bob"]=101;
        m["Alice"]=102;
        m["Eric"]=103;
        map<string,int>::iterator iter;
        for(iter=m.begin(); iter!=m.end(); iter++)
            cout<<iter->first <<"->"<<iter->second<<endl;
    }

    输出为:

    Alice->102
    Bob->101
    Eric->103

    可以看到map自动在内部以关键字为准,按字典序排序,而不是根据输入的顺序;

    需要注意的是 当我进行实验的时候 我发现这样一个现象:

    #include<bits/stdc++.h>
    
    using namespace std;
    
    int main()
    {
        map<string,int>m;
        m["Bob"]=101;
        m["Alice"]=102;
        m["Eric"]=103;
        map<string,int>::iterator iter;
        for(iter=m.begin(); iter!=m.end(); iter++)
            cout<<iter->first <<"->"<<iter->second<<endl;
            
        if(m["AAA"]==0)
        cout<<"NO"<<endl;
    
        for(iter=m.begin(); iter!=m.end(); iter++)
            cout<<iter->first <<"->"<<iter->second<<endl;
            
    
    }

    当询问一个map中不存在的数的时候,返回的值应该是0,不过当你再次遍历的时候,就会发现map中已经多了一个键值对,只不过值是0:

    Alice->102
    Bob->101
    Eric->103
    NO
    AAA->0
    Alice->102
    Bob->101
    Eric->103

    在做题时一定要好好注意。

    查找

    方法1:

    cout<<m.find("Bob")->second<<endl;

    如果按关键字搜索,搜不到的话会输出乱码

    方法2:

        map<string,int>::iterator iter1;
        iter1 = m.find(string("Bob"));
        if(iter1 != m.end())
        cout<<iter1->first <<"->"<<iter1->second<<endl;
        else    
        cout<<"no fount"<<endl;

    定义一个指针,指向map,如果没有的话会返回m.end()

    删除

    方法1

    m.erase(iter1);

    同样的是指针的操作

    方法2

    m.erase(string("AAA"));

    或者是根据关键字删除

    map的相关函数

    • 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的函数
  • 相关阅读:
    CentOS搭建nginx环境
    Gitment评论插件的使用
    GitPages部署自己的网站
    ubuntu防火墙规则之ufw
    鸟哥的Linux私房菜笔记第六章(二)
    一次使用InfluxDB数据库的总结
    网站实现markdown功能
    鸟哥的Linux私房菜笔记第六章(一)
    Flutter学习笔记(21)--TextField文本框组件和Card卡片组件
    Flutter学习笔记(20)--FloatingActionButton、PopupMenuButton、SimpleDialog、AlertDialog、SnackBar
  • 原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/11257335.html
Copyright © 2011-2022 走看看