zoukankan      html  css  js  c++  java
  • C++中unordered_map的使用

    unordered_map的使用

    unordered_map是类似于map的关联容器,其中存储的是键值对pair。哈希表的搜索、插入和元素移除拥有平均常数时间复杂度,元素在内部的存储是没有顺序的,而是通过键的哈希来确定元素具体放在具体的某个位置。

    unordered_map的常用函数

    函数名 函数作用
    empty 判定容器是否为空
    size 返回容器的元素
    max_size 返回可容纳的最大元素数
    clear 清除内容
    insert 插入元素或者结点
    insert_of_assign 插入元素,若当前元素已经存在则将该值赋予该元素
    emplace 原位构造元素
    try_emplace 若键不存在则原位插入,若键存在则不做任何事
    erase 擦除元素
    swap 交换内容
    at 访问指定位置的元素,并且进行越界检查
    count 返回匹配特定键的元素数量
    find 返回特定键的元素
    contains 判断哈希表中是否包含指定元素

    使用unordered_map存储普通类型数据

    使用unordered_map存储普通变量

    void TestUnordered_Map()
    {
        // use general type
        {
            std::unordered_map<int, std::string> name;
            name.insert(std::make_pair(1, "Alex"));
            name.insert(std::make_pair(2, "Alice"));
            name.insert(std::make_pair(3, "Alan"));
            name.insert  (std::make_pair(3, "Alan"));
    
            std::cout << "
    element of name is as follows-->
    ";
            for (auto const& val : name)
            {
                std::cout << "In name, first key is --> " << val.first << "	second value is --> " << val.second << std::endl;
            }
            std::cout << std::endl;
    
            // use find function
            std::unordered_map<int, std::string>::iterator itr;
            std::cout << "use find to judge key 2 whether exist
    ";
            if ((itr = name.find(2)) != name.end())
            {
                std::cout << "
    key = " << itr->first << " 	value = " << itr->second << std::endl;
            }
            std::cout << std::endl;
    
            // use size and empty function 
            if (!name.empty())
            {
                std::cout << "
    size of name is --> " << name.size() << std::endl;
            }
    
            // use count function to judge a element whether exist in this map
            if (name.count(3))
            {
                std::cout << "
    key value 3 exist in this map, and its value is --> " << name.at(3) << std::endl;
            }
    
            // use erase function to delete element
            if (name.count(2))
            {
                name.erase(2);
                std::cout << "
    size of name is --> " << name.size() << std::endl;
            }
    
            // use insert_of_assign function to overlap element
            name.insert_or_assign(3, "Bruce");
            for (const auto& val : name)
            {
                std::cout << "In name, first key is --> " << val.first << "	 second value is --> " << val.second << std::endl;
            }
            std::cout << std::endl;
    
            // use emplace insert element
            name.emplace(4, "Blex");
            name.emplace(std::make_pair(5, "Zee"));
            std::cout << "After insert two element, the size of name is --> " << name.size() << std::endl;
            for (const auto& val : name)
            {
                std::cout << "In name, first key is --> " << val.first << "	 second value is --> " << val.second << std::endl;
            }
            std::cout << std::endl;
        }
    }
    

    输出结果示例

    element of name is as follows-->
    In name, first key is --> 1     second value is --> Alex
    In name, first key is --> 2     second value is --> Alice
    In name, first key is --> 3     second value is --> Alan
    
    use find to judge key 2 whether exist
    
    key = 2         value = Alice
    
    size of name is --> 3
    
    key value 3 exist in this map, and its value is --> Alan
    
    size of name is --> 2
    In name, first key is --> 1      second value is --> Alex
    In name, first key is --> 3      second value is --> Bruce
    
    After insert two element, the size of name is --> 4
    In name, first key is --> 1      second value is --> Alex
    In name, first key is --> 3      second value is --> Bruce
    In name, first key is --> 4      second value is --> Blex
    In name, first key is --> 5      second value is --> Zee
    
  • 相关阅读:
    学习笔记-记ActiveMQ学习摘录与心得(一)
    c#写个基础的Socket通讯
    c#配置文件appStrings配置节的读取、添加和修改
    做个无边框winform窗体,并美化界面
    winform模拟鼠标按键
    winform程序开机自动启动代码
    记入园第一天
    HTTP Post 测试工具 (C#源代码)
    VC下调试内存泄漏的办法
    VMware共享文件夹遇到的问题
  • 原文地址:https://www.cnblogs.com/zuixime0515/p/13199687.html
Copyright © 2011-2022 走看看