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 << "\nelement of name is as follows-->\n";
        for (auto const& val : name)
        {
            std::cout << "In name, first key is --> " << val.first << "\tsecond 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\n";
        if ((itr = name.find(2)) != name.end())
        {
            std::cout << "\nkey = " << itr->first << " \tvalue = " << itr->second << std::endl;
        }
        std::cout << std::endl;

        // use size and empty function 
        if (!name.empty())
        {
            std::cout << "\nsize 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 << "\nkey 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 << "\nsize 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 << "\t 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 << "\t 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

猜你喜欢

转载自www.cnblogs.com/zuixime0515/p/13199687.html