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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM