当前位置:   article > 正文

C++——unordered_map讲解_c++ unorderedmap

c++ unorderedmap

unordered_map讲解

<unordered_map> 是 C++ 标准库中的一个头文件,提供了哈希表的实现,即无序关联容器。std::unordered_map 是一种关联容器,它使用哈希表来存储键值对,从而提供平均常数时间复杂度的查找、插入和删除操作。以下是对 <unordered_map> 详细讲解,包括其主要特性、使用方法和注意事项。

1. 引入头文件

#include <unordered_map>
  • 1

2. 基本概念

  • 键值对std::unordered_map 中的每个元素都是一个键值对(key-value pair),其中键是唯一的,值可以重复。
  • 哈希表std::unordered_map 使用哈希表来存储元素,通过哈希函数将键映射到桶中,从而提供快速的查找和更新操作。

3. 声明和初始化

#include <unordered_map>
#include <iostream>

int main() {
    std::unordered_map<int, std::string> umap;  // 创建一个空的 unordered_map
    std::unordered_map<int, std::string> umap2 = {{1, "one"}, {2, "two"}, {3, "three"}};  // 使用初始化列表
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4. 基本操作

插入元素
  • insert(): 插入键值对
  • emplace(): 原地构造键值对
#include <unordered_map>
#include <iostream>

int main() {
    std::unordered_map<int, std::string> umap;
    umap.insert({1, "one"});  // 使用 insert() 插入键值对
    umap.emplace(2, "two");   // 使用 emplace() 原地构造键值对
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
访问元素
  • operator[]: 通过键访问或插入元素
  • at(): 通过键访问元素,若键不存在则抛出异常
#include <unordered_map>
#include <iostream>

int main() {
    std::unordered_map<int, std::string> umap = {{1, "one"}, {2, "two"}};
    std::cout << umap[1] << std::endl;      // 输出 "one"
    std::cout << umap.at(2) << std::endl;   // 输出 "two"

    // 若键不存在,operator[] 会插入一个新的元素,at() 则会抛出异常
    std::cout << umap[3] << std::endl;      // 输出空字符串,并插入 {3, ""}
    try {
        std::cout << umap.at(4) << std::endl;  // 抛出异常
    } catch (const std::out_of_range& e) {
        std::cout << e.what() << std::endl;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
删除元素
  • erase(): 删除指定键的元素或删除指定位置的元素
#include <unordered_map>
#include <iostream>

int main() {
    std::unordered_map<int, std::string> umap = {{1, "one"}, {2, "two"}, {3, "three"}};
    umap.erase(2);  // 删除键为 2 的元素
    for (const auto& pair : umap) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
查找元素
  • find(): 查找键并返回指向该元素的迭代器
  • count(): 返回指定键的出现次数(对于 unordered_map,结果要么是 0 要么是 1)
#include <unordered_map>
#include <iostream>

int main() {
    std::unordered_map<int, std::string> umap = {{1, "one"}, {2, "two"}, {3, "three"}};
    auto it = umap.find(2);  // 查找键为 2 的元素
    if (it != umap.end()) {
        std::cout << "Found: " << it->first << ": " << it->second << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }

    std::cout << "Count of key 3: " << umap.count(3) << std::endl;  // 输出 1
    std::cout << "Count of key 4: " << umap.count(4) << std::endl;  // 输出 0
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
迭代器
  • begin(): 返回指向第一个元素的迭代器
  • end(): 返回指向最后一个元素后一位置的迭代器
#include <unordered_map>
#include <iostream>

int main() {
    std::unordered_map<int, std::string> umap = {{1, "one"}, {2, "two"}, {3, "three"}};
    for (auto it = umap.begin(); it != umap.end(); ++it) {
        std::cout << it->first << ": " << it->second << std::endl;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

5. 注意事项

  • 无序性std::unordered_map 中的元素无序存储,不能依赖元素的插入顺序或访问顺序。
  • 哈希函数:默认情况下,std::unordered_map 使用 std::hash 来计算键的哈希值。如果需要自定义哈希函数,可以在模板参数中指定。
  • 负载因子:哈希表的性能与负载因子相关。负载因子是元素数量与桶数量的比值。通过 load_factor() 可以获取当前负载因子,通过 max_load_factor() 可以设置最大负载因子。
  • 性能std::unordered_map 的平均时间复杂度是常数时间,但在最坏情况下可能退化为线性时间。选择合适的哈希函数和负载因子可以提高性能。

6. 总结

<unordered_map> 提供了高效的键值对存储和查找功能,是 C++ 标准库中非常有用的容器之一。通过掌握其基本操作和注意事项,可以在各种编程场景中高效地使用 std::unordered_map

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/955637
推荐阅读
相关标签
  

闽ICP备14008679号