当前位置 博文首页 > Linux猿:零基础都能看懂的 STL map 详解

    Linux猿:零基础都能看懂的 STL map 详解

    作者:[db:作者] 时间:2021-09-17 09:00


    🎈 作者:Linux猿

    🎈 简介:CSDN博客专家🏆,C/C++、面试、刷题、算法尽管咨询我,关注我,有问题私聊!

    🎈 关注专栏:C/C++面试通关集锦?(优质好文持续更新中……)🚀


    目录

    一、什么是 map ?

    二、map的定义

    2.1 头文件

    2.2 定义

    2.3 方法

    三、实例讲解

    3.1 增加数据

    3.2 删除数据

    3.3 修改数据

    3.4 查找数据

    3.5 遍历元素

    3.6 其它方法

    四、总结


    map 在编程中是经常使用的一个容器,本文来讲解一下 STL 中的 map,赶紧来看下吧!

    一、什么是 map ?

    map 是具有唯一键值对的容器,通常使用红黑树实现。

    map 中的键值对是 key value 的形式,比如:每个身份证号对应一个人名(反过来不成立哦!),其中,身份证号就是 key,人名便是 value,是单项的关系,可以与 hash 作类比。

    二、map的定义

    2.1 头文件

    使用 map 需要引入头文件,如下所示:

    #include <map>

    2.2 定义

    定义形式如下所示:

    map<key_type, value_type>变量名

    注意:如果没有 using namespace std, map需要写成 std:map。

    来看一个简单的例子:

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node;   // 定义变量
        node[123456] = "张三";
    
        cout<<"身份证号123456的人叫"<<node[123456]<<endl;
    }

    输出为:

    身份证号123456的人叫张三

    在上例中,定义了一个key 为 int ,value 为 string 的 map 容器 node。

    2.3 方法

    map 最常见的方法如下所示:

    //常用
    size()     // 计算元素个数
    empty()    // 判断是否为空,空返回 true
    clear()    // 清空容器
    erase()    // 删除元素
    find()     // 查找元素
    insert()   // 插入元素
    count()    // 计算指定元素出现的次数
    begin()    // 返回迭代器头部
    end()      // 返回迭代器尾部
    
    //非常用
    swap()        // 交换两个map容器,类型需要相同
    max_size()    // 容纳的最大元素个数
    rbegin()      // 指向map尾部的逆向迭代器
    rend()        // 指向map头部的逆向迭代器
    lower_bound() // 返回键值大于等于指定元素的第一个位置
    upper_bound() // 返回键值大于指定元素的第一个位置
    equal_range() // 返回等于指定元素的区间

    三、实例讲解

    3.1 增加数据

    方法1:以数组下标的形式直接增加,即:变量名[key] = value 的形式。

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node;   // 定义变量
        node[123456] = "张三";
        node[123457] = "李四";
        node[123458] = "王五";
    
        cout<<"身份证号123456的人叫"<<node[123456]<<endl;
        cout<<"身份证号123457的人叫"<<node[123457]<<endl;
        cout<<"身份证号123458的人叫"<<node[123458]<<endl;
    }

    输出为:

    身份证号123456的人叫张三
    身份证号123457的人叫李四
    身份证号123458的人叫王五

    方法2:直接插入键值对。

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node;   // 定义变量
    
        node.insert(pair<int, string>(123456, "张三"));
        node.insert(pair<int, string>(123457, "张三"));
        node.insert(pair<int, string>(123458, "李四"));
    
    
        cout<<"身份证号123456的人叫"<<node[123456]<<endl;
        cout<<"身份证号123457的人叫"<<node[123457]<<endl;
        cout<<"身份证号123458的人叫"<<node[123458]<<endl;
    }

    输出为:

    身份证号123456的人叫张三
    身份证号123457的人叫张三
    身份证号123458的人叫李四

    其中,pair 定义了一个键值对,对应 map 的 key 和 value。

    3.2 删除数据

    删除数据使用到 map 的 erase 和 clear方法,来看一下例子:

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node;   // 定义变量
    
        node[123456] = "张三";
        node[123457] = "李四";
        node[123458] = "王五";
        cout<<"size = "<<node.size()<<endl;
        //1. 使用 key 删除
        node.erase(123456);  // 删除 key = 123456 的节点
        cout<<"size = "<<node.size()<<endl;
        //2. 使用迭代器删除
        map<int,string>::iterator iter = node.find(123457);
        node.erase(iter);
        cout<<"size = "<<node.size()<<endl;
        //3. 清空整个容器
        node.clear();
        cout<<"size = "<<node.size()<<endl;
    }

    输出为:

    size = 3
    size = 2
    size = 1
    size = 0

    其中,clear 方法表示清空容器,size 方法表示获取容器大小。

    3.3 修改数据

    修改数据仅能修改 value 的值,key 是不能修改的,可以通过增加和删除来实现修改 key。

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node;   // 定义变量
    
        node[123456] = "张三";
        cout<<"身份证号123456的人叫"<<node[123456]<<endl;
        node[123456] = "李四";
        cout<<"身份证号123456的人叫"<<node[123456]<<endl;
    }

    输出为:

    身份证号123456的人叫张三
    身份证号123456的人叫李四

    3.4 查找数据

    查找数据通过 find 函数来实现,如下所示:

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node;   // 定义变量
    
        node[123456] = "张三";
        node[123457] = "李四";
        node[123458] = "王五";
        map<int, string>::iterator iter = node.find(123456);
        if(iter != node.end()) {
            cout<<"身份证号123456的人叫"<<iter->second<<endl;
        }
    }

    输出为:

    身份证号123456的人叫张三

    find 方法返回的是 map 的迭代器。?

    3.5 遍历元素

    遍历元素使用迭代器的方式,如下所示:

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node;   // 定义变量
    
        node[123456] = "张三";
        node[123457] = "李四";
        node[123458] = "王五";
        map<int, string>::iterator iter; //定义迭代器 iter
        for(iter = node.begin(); iter != node.end(); ++iter) {
            cout<<"身份证号"<<iter->first<<"的人叫"<<iter->second<<endl;
        }
    }

    输出为:

    身份证号123456的人叫张三
    身份证号123457的人叫李四
    身份证号123458的人叫王五

    其中,使用迭代器 iter 遍历容器,可以将迭代器理解为一个存储了 key 和 value 的一个结构,first 对应 key,second 对应 value。

    3.6 其它方法

    (1)swap 函数

    交换两个 map 容器的内容,map 容器的类型必须相同,例如:

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node1;   // 定义变量
        map<int, string>node2;
    
        node1[11] = "张三";
        node1[12] = "李四";
    
        node2[21] = "王五";
        node2[22] = "赵六";
        node2[23] = "孙七";
    
        node1.swap(node2);
        map<int, string>::iterator iter;
        cout<<"node1 :"<<endl;
        for(iter = node1.begin(); iter != node1.end(); ++iter) {
            cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
        }
    
        cout<<"node2 :"<<endl;
        for(iter = node2.begin(); iter != node2.end(); ++iter) {
            cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
        }
    }

    输出为:

    node1 :
    key = 21 value = 王五
    key = 22 value = 赵六
    key = 23 value = 孙七
    node2 :
    key = 11 value = 张三
    key = 12 value = 李四

    (2)max_size

    返回当前容器的可以容纳的最大元素个数,来看一个例子。

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node;   // 定义变量
    
        cout<<"max_size = "<<node.max_size()<<endl;
    
        node[11] = "张三";
        cout<<"max_size = "<<node.max_size()<<endl;
        
        node[12] = "李四";
        cout<<"max_size = "<<node.max_size()<<endl;
    
        node[13] = "王五";
        cout<<"max_size = "<<node.max_size()<<endl;
    
    }

    输出为:

    max_size = 128102389400760775
    max_size = 128102389400760775
    max_size = 128102389400760775
    max_size = 128102389400760775

    (3)rbegin 和 rend

    rbegin 和 rend 为反向迭代器,即:rbegin 指向最后一个元素,rend 指向第一个元素的前一个位置,来看一个例子。

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node;   // 定义变量
    
        node[11] = "张三";
        node[12] = "李四";
        node[13] = "王五";
        
        map<int, string>::reverse_iterator iter;
        for(iter = node.rbegin(); iter != node.rend(); ++iter) {
            cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
        }
    }

    输出为:

    key = 13 value = 王五
    key = 12 value = 李四
    key = 11 value = 张三

    注意:迭代器需要使用反向迭代器。

    (4)lower_bound 和 upper_bound

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node;   // 定义变量
    
        node[20] = "张三";
        node[15] = "李四";
        node[12] = "王五";
        
        map<int, string>::iterator iter = node.lower_bound(14);
        cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
    
        iter = node.upper_bound(12);
        cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
    }

    输出结果为:

    key = 15 value = 李四
    key = 15 value = 李四

    (5)equal_range

    #include <iostream>
    #include <map>  // 头文件
    #include <string>
    using namespace std;
    
    int main() {
        map<int, string>node;   // 定义变量
    
        node[12] = "张三";
        node[15] = "李四";
        node[20] = "王五";
        
        pair<map<int, string>::iterator, map<int, string>::iterator> p = node.equal_range(15);
    
        cout<<"key1 = "<<p.first->first<<" value1 = "<<p.first->second<<endl;
        cout<<"key2 = "<<p.second->first<<" value2 = "<<p.second->second<<endl;
    }

    输出为:

    key1 = 15 value1 = 李四
    key2 = 20 value2 = 王五

    四、总结

    编程中经常使用到 key / value 的形式表示数据之间的关系,故 map 是 STL 中经常使用的一个容器,需要记住 map 的常用方法。


    🎈 本文博主原创,创作不易,如果对您有帮助,欢迎小伙伴们点赞👍、收藏?、留言💬


    cs
    下一篇:没有了