ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

STL无序容器之unordered_map和unordered_multimap

2021-05-07 16:05:29  阅读:206  来源: 互联网

标签:map multimap 容器 umap 键值 include unordered


参考链接
map 容器中存储的数据是有序的,而 unordered_map 容器中是无序的。

unordered_map 容器在<unordered_map>头文件中,并位于 std 命名空间中。

参数含义
<key,T>前 2 个参数分别用于确定键值对中键和值的类型,也就是存储键值对的类型。
Hash = hash用于指明容器在存储各个键值对时要使用的哈希函数,默认使用 STL 标准库提供的 hash 哈希函数。注意,默认哈希函数只适用于基本数据类型(包括 string 类型),而不适用于自定义的结构体或者类。
Pred = equal_to要知道,unordered_map 容器中存储的各个键值对的键是不能相等的,而判断是否相等的规则,就由此参数指定。默认情况下,使用 STL 标准库中提供的 equal_to 规则,该规则仅支持可直接用 == 运算符做比较的数据类型。

当无序容器中存储键值对的键为自定义类型时,默认的哈希函数 hash 以及比较函数 equal_to 将不再适用,只能自己设计适用该类型的哈希函数和比较函数,并显式传递给 Hash 参数和 Pred 参数。

创建unordered_map容器的方法

  • 调用 unordered_map 模板类的默认构造函数,可以创建空的 unordered_map 容器。

    std::unordered_map<std::string, std::string> umap;
    
  • 在创建 unordered_map 容器的同时,可以完成初始化操作。

    std::unordered_map<std::string, std::string> umap{
        {"Python教程","http://c.biancheng.net/python/"},
        {"Java教程","http://c.biancheng.net/java/"},
        {"Linux教程","http://c.biancheng.net/linux/"} };
    
  • 调用 unordered_map 模板中提供的复制(拷贝)构造函数,将现有 unordered_map 容器中存储的键值对,复制给新建 unordered_map 容器。

    std::unordered_map<std::string, std::string> umap2(umap);
    

    以右值引用的方式将临时 unordered_map 容器中存储的所有键值对,全部复制给新建容器。

    //返回临时 unordered_map 容器的函数
    std::unordered_map <std::string, std::string > retUmap(){
        std::unordered_map<std::string, std::string>tempUmap{
            {"Python教程","http://c.biancheng.net/python/"},
            {"Java教程","http://c.biancheng.net/java/"},
            {"Linux教程","http://c.biancheng.net/linux/"} };
        return tempUmap;
    }
    //调用移动构造函数,创建 umap2 容器
    std::unordered_map<std::string, std::string> umap2(retUmap());
    
  • 使用 unordered_map 类模板提供的迭代器,在现有 unordered_map 容器中选择部分区域内的键值对,为新建 unordered_map 容器初始化。

    //传入 2 个迭代器,
    std::unordered_map<std::string, std::string> umap2(++umap.begin(),umap.end());
    

unordered_map容器的成员方法

成员方法功能
begin()返回指向容器中第一个键值对的正向迭代器。
end()返回指向容器中最后一个键值对之后位置的正向迭代器。
cbegin()和 begin() 功能相同,只不过在其基础上增加了 const 属性,即该方法返回的迭代器不能用于修改容器内存储的键值对。
cend()和 end() 功能相同,只不过在其基础上,增加了 const 属性,即该方法返回的迭代器不能用于修改容器内存储的键值对。
empty()若容器为空,则返回 true;否则 false。
size()返回当前容器中存有键值对的个数。
max_size()返回容器所能容纳键值对的最大个数,不同的操作系统,其返回值亦不相同。
operator[key]该模板类中重载了 [] 运算符,其功能是可以向访问数组中元素那样,只要给定某个键值对的键 key,就可以获取该键对应的值。注意,如果当前容器中没有以 key 为键的键值对,则其会使用该键向当前容器中插入一个新键值对。
at(key)返回容器中存储的键 key 对应的值,如果 key 不存在,则会抛出 out_of_range 异常。
find(key)查找以 key 为键的键值对,如果找到,则返回一个指向该键值对的正向迭代器;反之,则返回一个指向容器中最后一个键值对之后位置的迭代器(如果 end() 方法返回的迭代器)。
count(key)在容器中查找以 key 键的键值对的个数。
equal_range(key)返回一个 pair 对象,其包含 2 个迭代器,用于表明当前容器中键为 key 的键值对所在的范围。
emplace()向容器中添加新键值对,效率比 insert() 方法高。
emplace_hint()向容器中添加新键值对,效率比 insert() 方法高。
insert()向容器中添加新键值对。
erase()删除指定键值对。
clear()清空容器,即删除容器中存储的所有键值对。
swap()交换 2 个 unordered_map 容器存储的键值对,前提是必须保证这 2 个容器的类型完全相等。
bucket_count()返回当前容器底层存储键值对时,使用桶(一个线性链表代表一个桶)的数量。
max_bucket_count()返回当前系统中,unordered_map 容器底层最多可以使用多少桶。
bucket_size(n)返回第 n 个桶中存储键值对的数量。
bucket(key)返回以 key 为键的键值对所在桶的编号。
load_factor()返回 unordered_map 容器中当前的负载因子。负载因子,指的是的当前容器中存储键值对的数量(size())和使用桶数(bucket_count())的比值,即 load_factor() = size() / bucket_count()。
max_load_factor()返回或者设置当前 unordered_map 容器的负载因子。
rehash(n)将当前容器底层使用桶的数量设置为 n。
reserve()将存储桶的数量(也就是 bucket_count() 方法的返回值)设置为至少容纳count个元(不超过最大负载因子)所需的数量,并重新整理容器。
hash_function()返回当前容器使用的哈希函数对象。

equal_range(key) 很少用于 unordered_map 容器,因为该容器中存储的都是键不相等的键值对,即便调用该成员方法,得到的 2 个迭代器所表示的范围中,最多只包含 1 个键值对。事实上,该成员方法更适用于 unordered_multimap 容器

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
    //创建空 umap 容器
    unordered_map<string, string> umap;
    //向 umap 容器添加新键值对
    umap.emplace("Python教程", "http://c.biancheng.net/python/");
    umap.emplace("Java教程", "http://c.biancheng.net/java/");
    umap.emplace("Linux教程", "http://c.biancheng.net/linux/");
    //输出 umap 存储键值对的数量
    cout << "umap size = " << umap.size() << endl;
    //使用迭代器输出 umap 容器存储的所有键值对
    for (auto iter = umap.begin(); iter != umap.end(); ++iter) {
        cout << iter->first << " " << iter->second << endl;
    }
    return 0;
}

unordered_map迭代器的用法

unordered_map 容器迭代器的类型为前向迭代器(又称正向迭代器)。这意味着,假设 p 是一个前向迭代器,则其只能进行 *p、p++、++p 操作,且 2 个前向迭代器之间只能用 == 和 != 运算符做比较。

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
    //创建 umap 容器
    unordered_map<string, string> umap{
        {"Python教程","http://c.biancheng.net/python/"},
        {"Java教程","http://c.biancheng.net/java/"},
        {"Linux教程","http://c.biancheng.net/linux/"} };
    cout << "umap 存储的键值对包括:" << endl;
    //遍历输出 umap 容器中所有的键值对
    for (auto iter = umap.begin(); iter != umap.end(); ++iter) {
        cout << "<" << iter->first << ", " << iter->second << ">" << endl;
    }
    //获取指向指定键值对的前向迭代器
    unordered_map<string, string>::iterator iter = umap.find("Java教程");
    cout <<"umap.find(\"Java教程\") = " << "<" << iter->first << ", " << iter->second << ">" << endl;
    return 0;
}

在操作 unordered_map 容器过程(尤其是向容器中添加新键值对)中,一旦当前容器的负载因子超过最大负载因子(默认值为 1.0),该容器就会适当增加桶的数量(通常是翻一倍),并自动执行 rehash() 成员方法,重新调整各个键值对的存储位置(此过程又称“重哈希”),此过程很可能导致之前创建的迭代器失效。

所谓迭代器失效,针对的是那些用于表示容器内某个范围的迭代器,由于重哈希会重新调整每个键值对的存储位置,所以容器重哈希之后,之前表示特定范围的迭代器很可能无法再正确表示该范围。但是,重哈希并不会影响那些指向单个键值对元素的迭代器

#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
    //创建 umap 容器
    unordered_map<int, int> umap;
    //向 umap 容器添加 50 个键值对
    for (int i = 1; i <= 50; i++) {
        umap.emplace(i, i);
    }
    //获取键为 49 的键值对所在的范围
    auto pair = umap.equal_range(49);
    //输出 pair 范围内的每个键值对的键的值
    for (auto iter = pair.first; iter != pair.second; ++iter) {
        cout << iter->first <<" ";
    }
    cout << endl;
    //手动调整最大负载因子数
    umap.max_load_factor(3.0);
    //手动调用 rehash() 函数重哈希
    umap.rehash(10);
    //重哈希之后,pair 的范围可能会发生变化
    for (auto iter = pair.first; iter != pair.second; ++iter) {
        cout << iter->first << " ";
    }
    return 0;
}

unordered_map获取元素的4种方法

  • unordered_map 容器类模板中,实现了对 [ ] 运算符的重载,使得我们可以像“利用下标访问普通数组中元素”那样,通过目标键值对的键获取到该键对应的值。

    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    int main()
    {
        //创建 umap 容器
        unordered_map<string, string> umap{
            {"Python教程","http://c.biancheng.net/python/"},
            {"Java教程","http://c.biancheng.net/java/"},
            {"Linux教程","http://c.biancheng.net/linux/"} };
        //获取 "Java教程" 对应的值
        string str = umap["Java教程"];
        cout << str << endl;
        return 0;
    }
    

    如果当前容器中并没有存储以 [ ] 运算符内指定的元素作为键的键值对,则此时 [ ] 运算符的功能将转变为:向当前容器中添加以目标元素为键的键值对。

  • unordered_map 类模板中,还提供有 at() 成员方法,和使用 [ ] 运算符一样,at() 成员方法也需要根据指定的键,才能从容器中找到该键对应的值;不同之处在于,如果在当前容器中查找失败,该方法不会向容器中添加新的键值对,而是直接抛出out_of_range异常。

    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    int main()
    {
        //创建 umap 容器
        unordered_map<string, string> umap{
            {"Python教程","http://c.biancheng.net/python/"},
            {"Java教程","http://c.biancheng.net/java/"},
            {"Linux教程","http://c.biancheng.net/linux/"} };
        //获取指定键对应的值
        string str = umap.at("Python教程");
        cout << str << endl;
        //执行此语句会抛出 out_of_range 异常
        //cout << umap.at("GO教程");
        return 0;
    }
    
  • 借助 unordered_map 模板中提供的 find() 成员方法。

    • 当 find() 方法成功找到以指定元素作为键的键值对时,其返回的迭代器就指向该键值对;
    • 当 find() 方法查找失败时,其返回的迭代器和 end() 方法返回的迭代器一样,指向容器中最后一个键值对之后的位置。
    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    int main()
    {
        //创建 umap 容器
        unordered_map<string, string> umap{
            {"Python教程","http://c.biancheng.net/python/"},
            {"Java教程","http://c.biancheng.net/java/"},
            {"Linux教程","http://c.biancheng.net/linux/"} };
        //查找成功
        unordered_map<string, string>::iterator iter = umap.find("Python教程");
        cout << iter->first << " " << iter->second << endl;
        //查找失败
        unordered_map<string, string>::iterator iter2 = umap.find("GO教程");
        if (iter2 == umap.end()) {
            cout << "当前容器中没有以\"GO教程\"为键的键值对";
        }
        return 0;
    }
    
  • 借助 begin()/end() 或者 cbegin()/cend(),通过遍历整个容器中的键值对来找到目标键值对。

    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    int main()
    {
        //创建 umap 容器
        unordered_map<string, string> umap{
            {"Python教程","http://c.biancheng.net/python/"},
            {"Java教程","http://c.biancheng.net/java/"},
            {"Linux教程","http://c.biancheng.net/linux/"} };
        //遍历整个容器中存储的键值对
        for (auto iter = umap.begin(); iter != umap.end(); ++iter) {
            //判断当前的键值对是否就是要找的
            if (!iter->first.compare("Java教程")) {
                cout << iter->second << endl;
                break;
            }
        }
        return 0;
    }
    

unordered_map insert()用法

  • insert() 方法可以将 pair 类型的键值对元素添加到 unordered_map 容器中

    //以普通方式传递参数
    pair<iterator,bool> insert ( const value_type& val );
    //以右值引用的方式传递参数
    template <class P>
        pair<iterator,bool> insert ( P&& val );
    

    参数 val 表示要添加到容器中的目标键值对元素;该方法的返回值为 pair类型值,内部包含一个 iterator 迭代器和 bool 变量:

    • 当 insert() 将 val 成功添加到容器中时,返回的迭代器指向新添加的键值对,bool 值为 True;
    • 当 insert() 添加键值对失败时,意味着当前容器中本就存储有和要添加键值对的键相等的键值对,这种情况下,返回的迭代器将指向这个导致插入操作失败的迭代器,bool 值为 False。
    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    int main()
    {
        //创建空 umap 容器
        unordered_map<string, string> umap;
        //构建要添加的键值对
        std::pair<string, string>mypair("STL教程", "http://c.biancheng.net/stl/");
        //创建接收 insert() 方法返回值的pair类型变量
        std::pair<unordered_map<string, string>::iterator, bool> ret;
        //调用 insert() 方法的第一种语法格式
        ret = umap.insert(mypair);
        cout << "bool = " << ret.second << endl;
        cout << "iter -> " << ret.first->first <<" " << ret.first->second << endl;
       
        //调用 insert() 方法的第二种语法格式
        ret = umap.insert(std::make_pair("Python教程","http://c.biancheng.net/python/"));
        cout << "bool = " << ret.second << endl;
        cout << "iter -> " << ret.first->first << " " << ret.first->second << endl;
        return 0;
    }
    
  • insert() 方法还可以指定新键值对要添加到容器中的位置.

    //以普通方式传递 val 参数
    iterator insert ( const_iterator hint, const value_type& val );
    //以右值引用方法传递 val 参数
    template <class P>
        iterator insert ( const_iterator hint, P&& val );
    

    hint 参数为迭代器,用于指定新键值对要添加到容器中的位置;val 参数指的是要添加容器中的键值对;方法的返回值为迭代器:

    • 如果 insert() 方法成功添加键值对,该迭代器指向新添加的键值对;
    • 如果 insert() 方法添加键值对失败,则表示容器中本就包含有相同键的键值对,该方法返回的迭代器就指向容器中键相同的键值对;

    虽然通过 hint 参数指定了新键值对添加到容器中的位置,但该键值对真正存储的位置,并不是 hint 参数说了算,最终的存储位置仍取决于该键值对的键的值

    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    int main()
    {
        //创建空 umap 容器
        unordered_map<string, string> umap;
        //构建要添加的键值对
        std::pair<string, string>mypair("STL教程", "http://c.biancheng.net/stl/");
        //创建接收 insert() 方法返回值的迭代器类型变量
        unordered_map<string, string>::iterator iter;
        //调用第一种语法格式
        iter = umap.insert(umap.begin(), mypair);
        cout << "iter -> " << iter->first <<" " << iter->second << endl;
       
        //调用第二种语法格式
        iter = umap.insert(umap.begin(),std::make_pair("Python教程", "http://c.biancheng.net/python/"));
        cout << "iter -> " << iter->first << " " << iter->second << endl;
        return 0;
    }
    
  • insert() 方法还支持将某一个 unordered_map 容器中指定区域内的所有键值对,复制到另一个 unordered_map 容器中.

    template <class InputIterator>
        void insert ( InputIterator first, InputIterator last );
    
    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    int main()
    {
        //创建并初始化 umap 容器
        unordered_map<string, string> umap{ {"STL教程","http://c.biancheng.net/stl/"},
        {"Python教程","http://c.biancheng.net/python/"},
        {"Java教程","http://c.biancheng.net/java/"} };
        //创建一个空的 unordered_map 容器
        unordered_map<string, string> otherumap;
        //指定要拷贝 umap 容器中键值对的范围
        unordered_map<string, string>::iterator first = ++umap.begin();
        unordered_map<string, string>::iterator last = umap.end();
        //将指定 umap 容器中 [first,last) 区域内的键值对复制给 otherumap 容器
        otherumap.insert(first, last);
        //遍历 otherumap 容器中存储的键值对
        for (auto iter = otherumap.begin(); iter != otherumap.end(); ++iter){
            cout << iter->first << " " << iter->second << endl;
        }
        return 0;
    }
    
  • insert() 方法还支持一次向 unordered_map 容器添加多个键值对

    void insert ( initializer_list<value_type> il );
    
    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    int main()
    {
        //创建空的 umap 容器
        unordered_map<string, string> umap;
        //向 umap 容器同时添加多个键值对
        umap.insert({ {"STL教程","http://c.biancheng.net/stl/"},
        {"Python教程","http://c.biancheng.net/python/"},
        {"Java教程","http://c.biancheng.net/java/"} });
        //遍历输出 umap 容器中存储的键值对
        for (auto iter = umap.begin(); iter != umap.end(); ++iter){
            cout << iter->first << " " << iter->second << endl;
        }
        return 0;
    }
    

unordered_map emplace()和emplace_hint()方法

它们完成“向容器中添加新键值对”的效率,要比 insert() 方法高。

emplace()

template <class... Args>
    pair<iterator, bool> emplace ( Args&&... args );

参数 args 表示可直接向该方法传递创建新键值对所需要的 2 个元素的值,其中第一个元素将作为键值对的键,另一个作为键值对的值。也就是说,该方法无需我们手动创建键值对,其内部会自行完成此工作。

该方法的返回值为 pair 类型值,其包含一个迭代器和一个 bool 类型值:

  • 当 emplace() 成功添加新键值对时,返回的迭代器指向新添加的键值对,bool 值为 True;
  • 当 emplace() 添加新键值对失败时,说明容器中本就包含一个键相等的键值对,此时返回的迭代器指向的就是容器中键相同的这个键值对,bool 值为 False。
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
    //创建 umap 容器
    unordered_map<string, string> umap;
    //定义一个接受 emplace() 方法的 pair 类型变量
    pair<unordered_map<string, string>::iterator, bool> ret;
    //调用 emplace() 方法
    ret = umap.emplace("STL教程", "http://c.biancheng.net/stl/");
    //输出 ret 中包含的 2 个元素的值
    cout << "bool =" << ret.second << endl;
    cout << "iter ->" << ret.first->first << " " << ret.first->second << endl;
    return 0;
}

emplace_hint()

template <class... Args>
    iterator emplace_hint ( const_iterator position, Args&&... args );

emplace_hint() 方法内部会自行构造新键值对,因此我们只需向其传递构建该键值对所需的 2 个元素(第一个作为键,另一个作为值)即可。不同之处在于:

  • emplace_hint() 方法的返回值仅是一个迭代器,而不再是 pair 类型变量。当该方法将新键值对成功添加到容器中时,返回的迭代器指向新添加的键值对;反之,如果添加失败,该迭代器指向的是容器中和要添加键值对键相同的那个键值对。
  • emplace_hint() 方法还需要传递一个迭代器作为第一个参数,该迭代器表明将新键值对添加到容器中的位置。需要注意的是,新键值对添加到容器中的位置,并不是此迭代器说了算,最终仍取决于该键值对的键的值。
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
    //创建 umap 容器
    unordered_map<string, string> umap;
    //定义一个接受 emplace_hint() 方法的迭代器
    unordered_map<string,string>::iterator iter;
    //调用 empalce_hint() 方法
    iter = umap.emplace_hint(umap.begin(),"STL教程", "http://c.biancheng.net/stl/");
    //输出 emplace_hint() 返回迭代器 iter 指向的键值对的内容
    cout << "iter ->" << iter->first << " " << iter->second << endl;
    return 0;
}

unordered_map删除元素:erase()和clear()

erase()

  • erase() 方法可以接受一个正向迭代器,并删除该迭代器指向的键值对。

    iterator erase ( const_iterator position );
    
    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    int main()
    {
        //创建 umap 容器
        unordered_map<string, string> umap{
            {"STL教程", "http://c.biancheng.net/stl/"},
            {"Python教程", "http://c.biancheng.net/python/"},
            {"Java教程", "http://c.biancheng.net/java/"} };
        //输出 umap 容器中存储的键值对
        for (auto iter = umap.begin(); iter != umap.end(); ++iter) {
            cout << iter->first << " " << iter->second << endl;
        }
        cout << "erase:" << endl;
        //定义一个接收 erase() 方法的迭代器
        unordered_map<string,string>::iterator ret;
        //删除容器中第一个键值对
        ret = umap.erase(umap.begin());
        //输出 umap 容器中存储的键值对
        for (auto iter = umap.begin(); iter != umap.end(); ++iter) {
            cout << iter->first << " " << iter->second << endl;
        }
        cout << "ret = " << ret->first << " " << ret->second << endl;
        return 0;
    }
    

    通过给 erase() 方法传入指向容器中第一个键值对的迭代器,该方法可以将容器中第一个键值对删除,同时返回一个指向被删除键值对之后位置的迭代器。

    如果erase()方法删除的是容器存储的最后一个键值对,则该方法返回的迭代器,将指向容器中最后一个键值对之后的位置(等同于 end() 方法返回的迭代器)。

  • 直接将要删除键值对的键作为参数直接传给 erase() 方法,该方法会自行去 unordered_map 容器中找和给定键相同的键值对,将其删除。

    size_type erase ( const key_type& k );
    

    k 表示目标键值对的键的值;该方法会返回一个整数,其表示成功删除的键值对的数量。

    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    int main()
    {
        //创建 umap 容器
        unordered_map<string, string> umap{
            {"STL教程", "http://c.biancheng.net/stl/"},
            {"Python教程", "http://c.biancheng.net/python/"},
            {"Java教程", "http://c.biancheng.net/java/"} }; 
        //输出 umap 容器中存储的键值对
        for (auto iter = umap.begin(); iter != umap.end(); ++iter) {
            cout << iter->first << " " << iter->second << endl;
        }
        int delNum = umap.erase("Python教程");
        cout << "delNum = " << delNum << endl;
        //再次输出 umap 容器中存储的键值对
        for (auto iter = umap.begin(); iter != umap.end(); ++iter) {
            cout << iter->first << " " << iter->second << endl;
        }
        return 0;
    }
    
  • erase() 方法还支持一次删除指定范围内的所有键值对

    iterator erase ( const_iterator first, const_iterator last );
    
    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    int main()
    {
        //创建 umap 容器
        unordered_map<string, string> umap{
            {"STL教程", "http://c.biancheng.net/stl/"},
            {"Python教程", "http://c.biancheng.net/python/"},
            {"Java教程", "http://c.biancheng.net/java/"} };
        //first 指向第一个键值对
        unordered_map<string, string>::iterator first = umap.begin();
        //last 指向最后一个键值对
        unordered_map<string, string>::iterator last = --umap.end();
        //删除[fist,last)范围内的键值对
        auto ret = umap.erase(first, last);
        //输出 umap 容器中存储的键值对
        for (auto iter = umap.begin(); iter != umap.end(); ++iter) {
            cout << iter->first << " " << iter->second << endl;
        }
        cout << "ret -> " << ret->first << " " << ret->second << endl;
        return 0;
    }
    

clear()

一次性删除 unordered_map 容器中存储的所有键值对

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
    //创建 umap 容器
    unordered_map<string, string> umap{
        {"STL教程", "http://c.biancheng.net/stl/"},
        {"Python教程", "http://c.biancheng.net/python/"},
        {"Java教程", "http://c.biancheng.net/java/"} };
    //输出 umap 容器中存储的键值对
    for (auto iter = umap.begin(); iter != umap.end(); ++iter) {
        cout << iter->first << " " << iter->second << endl;
    }
    //删除容器内所有键值对
    umap.clear();
    cout << "umap size = " << umap.size() << endl;
    return 0;
}

unordered_multimap容器

unordered_multimap 容器和 unordered_map 容器一样,定义在<unordered_map>头文件,且位于 std 命名空间中。

创建C++ unordered_multimap容器的方法和unordered_map类似,此处省略。

unordered_multimap容器的成员方法和 unordered_map 容器相比,unordered_multimap 容器的类模板中没有重载 [ ] 运算符,也没有提供 at() 成员方法,除此之外它们完全一致。

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
    //创建空容器
    std::unordered_multimap<std::string, std::string> myummap;
    //向空容器中连续添加 5 个键值对
    myummap.emplace("Python教程", "http://c.biancheng.net/python/");
    myummap.emplace("STL教程", "http://c.biancheng.net/stl/");
    myummap.emplace("Java教程", "http://c.biancheng.net/java/");
    myummap.emplace("C教程", "http://c.biancheng.net");
    myummap.emplace("C教程", "http://c.biancheng.net/c/");
    //输出 muummap 容器存储键值对的个数
    cout << "myummmap size = " << myummap.size() << endl;
    //利用迭代器输出容器中存储的所有键值对
    for (auto iter = myummap.begin(); iter != myummap.end(); ++iter) {
        cout << iter->first << " " << iter->second << endl;
    }
    return 0;
}

标签:map,multimap,容器,umap,键值,include,unordered
来源: https://blog.csdn.net/andyjkt/article/details/116493853

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有