unordered_map賦值以及常用成員函數


unordered_map

1. 賦值操作

賦值比較簡單,和其他STL都差不多的。

#include <iostream>
#include <unordered_map>
using namespace std;
int main() 
{
    unordered_map<string, string> p1; // 直接定義
    unordered_map<string, string> p2{ {"apple", "red"}, {"lemon", "yellow"} }; // 直接在定義后賦值
    unordered_map<string, string> p3(p2); // 拷貝p2給p3
    unordered_map<string, string> p4(p3.begin(), p3.end()); // 通過迭代器一一賦值
    unordered_map<string, string> p5 = p4; // 通過賦值符號直接拷貝
    unordered_map<string, string> p6 = { {"apple", "red"}, {"lemon", "yellow"} }; // 通過賦值符號直接賦值
    system("pause");
    return 0;
}

注:后續還有可以達到賦值效果的成員函數

2. 成員函數

2.1 元素訪問

1) operator[]

2) at()

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() 
{
    unordered_map<int, string> p1 = { {1, "這是一"}, {2, "這是二"}};
    cout << "p1[1] = " << p1[1] << endl; // 通過operator[]可以直接訪問該鍵的值
    p1[1] = "這是1"; // 在訪問鍵的時候可以用賦值符號進行修改
    cout << "p1[1]修改后:" << p1[1] << endl;
    p1[3] = "這是三"; // 訪問一個不存在的鍵可以直接添加鍵值對
    p1[4];            // 如果在訪問的時候沒有賦值,則只添加鍵,值為空
    cout << "p1[3] = " << p1[3] << endl;
    cout << "p1[4] = " << p1[4] << endl;
    cout << "p1.at(3) = " << p1.at(3) << endl; // 通過成員函數at訪問鍵值對
    system("pause");
    return 0;
}

輸出結果為:

p1[1] = 這是一
p1[1]=修改后:這是1
p1[3] = 這是三
p1[4] = 
p1.at(3) = 這是三

注:at()訪問成員函數不能像[]一樣對鍵值對進行添加,但是可以修改鍵所對應的值,所以[]更常用

2.2 容量

1) empty()

如果map為空,則返回true,否則返回false

2)size()

返回map中鍵的個數(值可以為空,但是鍵不可以)

3) max_size()

返回容器可用的最大值

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() 
{
    unordered_map<int, string> p1 = { {1, "這是一"}, {2, "這是二"}, {3, "這是三"} };
    if (!p1.empty()) {
        cout << p1.size() << endl;
        cout << p1.max_size() << endl;
    }
    system("pause");
    return 0;
}

輸出結果為:

3
107374182 // 不一定相同

2.3 迭代器

1) begin()

  • 返回一個迭代器,指向容器中/一個桶中的第一個元素
  • ()中不添加參數時,返回指向容器中第一個元素的迭代器,()中可填入桶數,此時返回指向該桶中第一個元素的迭代器

2) end()

  • 返回一個迭代器,指向容器中/一個桶中的最后一個元素的下一位,所以end()返回的迭代器是不指向任何元素的
  • 常用for(auto ite=map.begin();ite!=map.end();++ite)的方式循環遍歷
  • 參數機制與begin的相同

3) cbegin()

  • 返回一個常量迭代器,指向容器中/一個桶中的第一個元素
  • 常量迭代器本身是可以進行加減操作的,上述的遍歷方式他也可以,只是不能修改迭代器中的值,類似一個指針常量
  • 參數機制與begin相同

4) cend()

  • 返回一個常量迭代器,指向容器中/一個桶中的最后一個元素的下一位
  • 參數機制與begin相同
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() 
{
    unordered_map<int, string> p1 = { {1, "這是一"}, {2, "這是二"}, {3, "這是三"} };
    // unordered_map<int, string>::iterator ite 可簡寫為 auto ite
    for (unordered_map<int, string>::iterator ite = p1.begin(); ite != p1.end(); ++ite) {
        cout << ite->first << ": " << ite->second << endl;
    }
    // 更方便的一種遍歷容器的方式
    //for (auto& i : p1) {
    //    cout << i.first << ": " << i.second << endl;
    //}
    system("pause");
    return 0;
}

輸出結果為:

1: 這是1
2: 這是2
3: 這是3

2.4 查看元素

1) find()

  • 參數為需要查找的鍵,返回該鍵所對應的迭代器
  • 如果未找到,則返回end()迭代器

2) count()

  • 參數為需要查找的鍵,返回該鍵出現的次數
  • 因為map的鍵不可重復,所以存在則返回1,不存在則返回0
  • 該函數可在unordered_multimap中返回鍵的個數

3) equal_range()

  • 參數為鍵值,返回一個滿足要求的范圍,具體看代碼理解一下就好
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
int main() 
{
    unordered_map<int, string> p1 = { {1, "這是一"}, {2, "這是二"}, {3, "這是三"} };
    unordered_map<int, string> p2(p1.find(2), ++p1.find(2));
    cout << "p2中有: ";
    for (auto& i : p2) {
        cout << i.first << ": " << i.second << endl;
    }
    cout << "p1中鍵1的個數為: " << p1.count(1) << endl;
    cout << "p2中鍵1的個數為: " << p2.count(1) << endl;
    auto range = p1.equal_range(1);
    cout << "滿足equal_range(1)的值為: ";
    for_each(
        range.first,
        range.second,
        [](unordered_map<int, string>::value_type& x) {cout << x.second << endl; }
    );
    system("pause");
    return 0;
}

輸出結果為:

p2中有: 2: 這是二
p1中鍵1的個數為: 1
p2中鍵1的個數為: 0
滿足equal_range(1)的值為: 這是一

2.5 元素修改

1) emplace()

  • 主要是用來添加或者修改一個值
  • 參數為一個鍵值對,若表中無該鍵,則直接添加進去,如果有鍵且有對應的值,則不改變原來的值

2) insert()

  • insert相比emplace可以插入多個值(具體看代碼即可)
  • 參數可以是一個pair變量,make_pair或者直接需要加入的鍵值對

3) erase()

  • 用來刪除表中的一個或者多個值
  • 參數為鍵或者迭代器都可以刪除一個值,當參數為迭代器的左閉右開范圍時可以刪除多個值(看代碼理解即可)

4) clear()

  • 清空表中所有鍵值對

5) swap()

  • 將兩個表中的內容交換
  • 參數為另一個需要交換的map
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

int main()
{
    unordered_map<int, string> p1 = { {1, "這是一"}, {2, "這是二"}, {3, "這是三"} };
    // emplace()
    p1.emplace(1, "這是1"); // 存在則不改變
    p1.emplace(4, "這是四");// 不存在則插入
    cout << "emplace()插入后: " << endl;
    for (auto& i : p1) {
        cout << i.first << ": " << i.second << endl;
    }
    // insert()
    pair<int, string> p(5, "這是五");
    p1.insert(p); // 插入一個pair變量
    p1.insert(make_pair<int, string>(6, "這是六")); // 直接make_pair
    p1.insert({1, "這是1"}); // 直接插入(存在也不改變)
    cout << "insert()插入后: " << endl;
    for (auto& i : p1) {
        cout << i.first << ": " << i.second << endl;
    }
    // erase()
    p1.erase(1);// 直接刪除鍵值對
    cout << "earse(1)之后:  " << endl;
    for (auto& i : p1) {
        cout << i.first << ": " << i.second << endl;
    }
    p1.erase(p1.begin());// 刪除第一個迭代器的鍵值對
    cout << "erase(p1.begin())之后:  " << endl;
    for (auto& i : p1) {
        cout << i.first << ": " << i.second << endl;
    }
    p1.erase(p1.find(5), p1.end());// 刪除從find(5)開始到end()之間的鍵值對
    cout << "erase(p1.find(5), p1.end()):  " << endl;
    for (auto& i : p1) {
        cout << i.first << ": " << i.second << endl;
    }
    // clear()
    p1.clear();
    cout << "clear()之后: " << endl;
    for (auto& i : p1) {
        cout << i.first << ": " << i.second << endl;
    }
    // swap()
    unordered_map<int, string> p2 = { {1, "這是1"}, {2, "這是1"}, {3, "這是1"} };
    unordered_map<int, string> p3 = { {1, "這是一"}, {2, "這是二"}, {3, "這是三"} };
    cout << "交換前p2: " << endl;
    for (auto& i : p2) {
        cout << i.first << ": " << i.second << endl;
    }
    cout << "交換前p3: " << endl;
    for (auto& i : p3) {
        cout << i.first << ": " << i.second << endl;
    }
    p2.swap(p3);
    cout << "交換后p2: " << endl;
    for (auto& i : p2) {
        cout << i.first << ": " << i.second << endl;
    }
    cout << "交換后p3: " << endl;
    for (auto& i : p3) {
        cout << i.first << ": " << i.second << endl;
    }
    system("pause");
    return 0;
}

輸出結果為:

empIace()插入后:
1:這是一
2:這是二
3:這是三
4:這是四
insert()插入后:
1:這是一
2:這是二
3:這是三
4:這是四
5:這是五
6:這是六
earse(1)之后:
2:這是二
3:這是三
4:這是四
5:這是五
6:這是六
erase(p1.begin())之后:
3:這是三
4:這是四
5:這是五
6:這是六
erase(pl.find(5), p1.end())之后
3:這是三
4:這是四
clear()之后:
交換前p2:
1:這是1
2:這是1
3:這是1
交換前p3:
1:這是一
2:這是二
3:這是三
交換后p2:
1:這是一
2:這是二
3:這是三
交換后p3:
1:這是1
2:這是1
3:這是1

2.6 桶

1) buket_count()

  • 返回map當前的桶數,會根據元素的數量進行變化

2) max_buket_count()

  • 返回容量最大的桶數

3) bucket_size()

  • 參數為桶,返回桶中的元素

4) buket()

  • 參數為鍵,返回該鍵所在的桶的序號
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
    unordered_map<int, string> p1 = { {1, "這是一"}, {2, "這是二"}, {3, "這是三"} };
    cout << "max_bucket_count() = " << p1.max_bucket_count() << endl;
    cout << "bucket_count() = " << p1.bucket_count() << endl;
    for (unsigned i = 0; i < p1.bucket_count(); i++) {
        cout << "bucket #" << i << " contains:";
        for (auto ite = p1.begin(i); ite != p1.end(i); ite++) {
            cout << ite->first << ": " << ite->second << " ";
        }
        cout << endl;
    }
    cout << "1這個鍵所在的bucket的大小為: " << p1.bucket_size(p1.bucket(1)) << endl;
    system("pause");
    return 0;
}

輸出結果為:

max_bucket_count() = 536870911
bucket_count() = 8
bucket #0 contains :
bucket #1 contains :
bucket #2 contains :
bucket #3 contains :
bucket #4 contains : 1 : 這是一
bucket #5 contains :
bucket #6 contains : 3 : 這是三
bucket #7 contains : 2 : 這是二
1這個鍵所在的bucket的大小為 :1


免責聲明!

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



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