C++ unordered_map

这篇博客详细介绍了C++11中的unordered_map容器,包括其无序性、查找元素的方法(如at()、operator[]、count()、find()、contains())、迭代器操作、容量管理(如empty()、size()、max_size())、修改操作(如clear()、insert()、emplace()等)、哈希策略以及观察器函数。unordered_map提供了高效的元素查找和插入操作,适用于需要快速访问的数据结构。
摘要由CSDN通过智能技术生成

转载自 这里

目录

查找元素

迭代器

容量

修改操作

通接口

哈希策略

观察器

std::swap(std::unordered_map)

std::erase_if (std::unordered_map)


简介

本篇博客介绍C++常用的无序关联容器unordered_map。unordered_map是C++11正式加入的对hash_map的官方实现(之前标准C++没有hash_map的官方实现,我们使用的STL的hash_map并不是官方的)。从名字可以看出这个结构是无序的,底层设计思想和STL的hash_map一样。元素在内部不以任何特定顺序排序,而是放进桶中。元素放进哪个桶完全依赖于其键的哈希。这允许对单独元素的快速访问,因为一旦计算哈希,则它准确指代元素所放进的桶。unordered_map搜索、插入和元素移除拥有平均常数时间复杂度。

 

查找元素

at()用于访问指定元素

 
  1. T& at( const Key& key );

  2. const T& at( const Key& key ) const;

返回到等于 key 的键的元素的引用。若无该元素,则抛出 std::out_of_range 类型异常。

 

operator []用于访问或插入元素

 
  1. T& operator[]( const Key& key );

  2. T& operator[]( Key&& key );

返回等于 key 的键的值的引用。如果该键不存在,则执行插入,反之,则覆盖原来的值。

 
  1. #include <iostream>

  2. #include <string>

  3. #include <vector>

  4. #include <unordered_map>

  5.  
  6. int main()

  7. {

  8. std::unordered_map<char, int> letter_counts { {'a', 27}, {'b', 3}, {'c', 1}};

  9.  
  10. std::cout << "initially:\n";

  11. for (const auto &pair : letter_counts) {

  12. std::cout << pair.first << ": " << pair.second << '\n';

  13. }

  14.  
  15. letter_counts['b'] = 42; // 更新既存值

  16. letter_counts['x'] = 9; // 插入新值

  17.  
  18. std::cout << "after modifications:\n";

  19. for (const auto &pair : letter_counts) {

  20. std::cout << pair.first << ": " << pair.second << '\n';

  21. }

  22.  
  23. // 统计每个词的出现数

  24. // (首次调用 operator[] 以零初始化计数器)

  25. std::unordered_map<std::string, size_t> word_map;

  26. for (const auto &w : { "this", "sentence", "is", "not", "a", "sentence",

  27. "this", "sentence", "is", "a", "hoax"}) {

  28. ++word_map[w];

  29. }

  30.  
  31. for (const auto &pair : word_map) {

  32. std::cout << pair.second << " occurrences of word '" << pair.first << "'\n";

  33. }

  34. }

  35.  
  36. 输出:

  37. initially:

  38. a: 27

  39. b: 3

  40. c: 1

  41. after modifications:

  42. a: 27

  43. b: 42

  44. c: 1

  45. x: 9

  46. 2 occurrences of word 'a'

  47. 1 occurrences of word 'hoax'

  48. 2 occurrences of word 'is'

  49. 1 occurrences of word 'not'

  50. 3 occurrences of word 'sentence'

  51. 2 occurrences of word 'this'

 

count() 返回特定键的元素数量

 
  1. (1)size_type count( const Key& key ) const;

  2. (2)template< class K > 

  3. size_type count( const K& x ) const;

(1) 返回等于指定参数 key 的键的元素数,因为此容器不允许重复,故返回值为 0 或 1 。

(2) 返回键等于指定参数 x 的元素数。此重载仅若有限定标识 Hash::is_transparent 与 KeyEqual::is_transparent 均合法并指代类型才参与重载决议。这假设能用 K 和 Key 类型一起调用这种 Hash ,还有 KeyEqual 是通透的,进而允许不用构造 Key 的实例就调用此函数。

 

find()寻找特定键的元素

 
  1. (1)iterator find( const Key& key );

  2. (2)const_iterator find( const Key& key ) const;

  3. (3)template< class K > iterator find( const K& x );

  4. (4)template< class K > const_iterator find( const K& x ) const;

(1-2) 寻找键等于 key 的的元素。

(3-4) 寻找键等于值 x 的元素。此重载仅若有限定标识 Hash::is_transparent 与 KeyEqual::is_transparent 均合法并指代类型才参与重载决议。这假设能用 K 和 Key 类型一起调用这种 Hash ,还有 KeyEqual 是通透的,进而允许不用构造 Key 的实例就调用此函数。

 
  1. #include <iostream>

  2. #include <unordered_map>

  3.  
  4. int main()

  5. {

  6. // 简单比较演示

  7. std::unordered_map<int,char> example = { {1,'a'},{2,'b'}};

  8.  
  9. auto search = example.find(2);

  10. if (search != example.end()) {

  11. std::cout << "Found " << search->first << " " << search->second << '\n';

  12. } else {

  13. std::cout << "Not found\n";

  14. }

  15. }

  16.  
  17. 输出:

  18. Found 2 b

 

contains()可检查容器是否含有特定键的元素

 
  1. (1)bool contains( const Key& key ) const;

  2. (2)template< class K > bool contains( const K& x ) const;

(1) 检查容器中是否有键等于 key 的元素。

(2) 检查是否有键等于值 x 的元素。此重载仅若有限定标识 Hash::is_transparent 与 KeyEqual::is_transparent 均合法并指代类型才参与重载决议。这假设能用 K 和 Key 类型一起调用这种 Hash ,还有 KeyEqual 是通透的,进而允许不用构造 Key 的实例就调用此函数。

 
  1. #include <iostream>

  2. #include <unordered_map>

  3.  
  4. int main()

  5. {

  6. std::unordered_map<int,char> example = { {1,'a'},{2,'b'}};

  7.  
  8. if (example.contains(2)) {

  9. std::cout << "Found\n";

  10. } else {

  11. std::cout << "Not found\n";

  12. }

  13. }

  14.  
  15. 输出:

  16. Found

find()的返回值是迭代器,若有元素,可以直接通过返回的迭代器获取元素信息。 而contains()的返回值是bool型,只用于确定是否包含该元素。

 

equal_range()返回匹配特定键的元素范围

 
  1. (1)std::pair<iterator,iterator> equal_range( const Key& key );

  2. (2)std::pair<const_iterator,const_iterator> equal_range( const Key& key ) const;

  3. (3)template< class K >

  4. std::pair<iterator,iterator> equal_range( const K& x );

  5. (4)template< class K >

  6. std::pair<const_iterator,const_ite

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值