c++中 unordered_map 与 unordered_set 用法指南

2024-06-02 03:28

本文主要是介绍c++中 unordered_map 与 unordered_set 用法指南,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

unordered_map 与 unordered_set 区别与联系

unordered_mapunordered_set 都是 C++ 标准模板库(STL)中的容器,它们使用哈希表作为底层数据结构,提供了快速的查找、插入和删除操作。下面是它们之间的联系与区别:

联系

  1. 底层实现:两者都基于哈希表实现,利用了哈希函数来分布元素。
  2. 性能特点:它们都提供平均时间复杂度为 O(1) 的查找、插入和删除操作。
  3. 冲突解决:两者都使用某种形式的链表或红黑树来解决哈希冲突。
  4. 非线性容器:它们都是非线性容器,不保证元素的顺序。

区别

  1. 存储内容

    • unordered_map 存储键值对,即每个元素包含一个键(key)和一个值(value),键是唯一的。
    • unordered_set 只存储唯一的值,不包含键。
  2. 用途

    • unordered_map 适用于需要通过键来访问或存储数据的场景,类似于关联数组或字典。
    • unordered_set 适用于需要存储唯一元素集合的场景,类似于集合。
  3. 操作

    • unordered_map 支持通过键来访问、插入、删除和查找值。
    • unordered_set 支持插入、删除和查找元素。
  4. 内存使用

    • unordered_map 由于需要存储键和值,通常比 unordered_set 使用更多的内存。
  5. 迭代器

    • unordered_map 的迭代器可以解引用为一个 pair,其中包含键和值。
    • unordered_set 的迭代器只能解引用为一个值。
  6. 示例
    在这里插入图片描述

  7. 元素查找

    • unordered_mapfind 方法返回一个迭代器,指向键值对,可以访问键和值。
    • unordered_setfind 方法返回一个迭代器,指向集合中的元素。

使用场景

  • 当你需要一个键来快速访问数据时,使用 unordered_map
  • 当你只需要存储一组不包含重复元素的数据时,使用 unordered_set

总的来说,unordered_mapunordered_set 在实现上有很多相似之处,但它们服务于不同的数据存储需求。选择使用哪一个取决于你的具体应用场景和需求。

unordered_map

unordered_map 是 C++ 中的一个关联容器,它存储了键值对,并且提供了快速的数据访问能力。

  1. 基本使用
#include <iostream>
#include <unordered_map>int main() {std::unordered_map<int, std::string> umap;umap[1] = "one";umap[2] = "two";umap[3] = "three";// 打印所有键值对for (const auto& pair : umap) {std::cout << pair.first << ": " << pair.second << std::endl;}
}
  • 输出结果:

1: one
2: two
3: three
在这里插入图片描述

  1. 初始化列表
#include <iostream>
#include <unordered_map>int main() {std::unordered_map<int, std::string> umap = {{1, "one"},{2, "two"},{3, "three"}};// 打印所有键值对for (const auto& pair : umap) {std::cout << pair.first << ": " << pair.second << std::endl;}
}
  • 输出结果:

1: one
2: two
3: three
在这里插入图片描述

  1. 访问元素
#include <iostream>
#include <unordered_map>int main() {std::unordered_map<int, std::string> umap = {{1, "one"},{2, "two"}};// 访问元素std::cout << "Value for key 1: " << umap[1] << std::endl;// 访问不存在的键将自动插入该键,并为其分配一个默认值std::cout << "Value for key 3: " << umap[3] << std::endl; // 默认值,例如空字符串
}
  • 输出结果:

Value for key 1: one
Value for key 3:
在这里插入图片描述

  1. 检查键是否存在
#include <iostream>
#include <unordered_map>int main() {std::unordered_map<int, std::string> umap = {{1, "one"},{2, "two"}};int key = 1;if (umap.find(key) != umap.end()) {std::cout << "Key " << key << " exists." << std::endl;}else {std::cout << "Key " << key << " does not exist." << std::endl;}
}
  • 输出结果:

Key 1 exists.
在这里插入图片描述

  1. 删除元素
#include <iostream>
#include <unordered_map>int main() {std::unordered_map<int, std::string> umap = {{1, "one"},{2, "two"}};int keyToRemove = 1;auto it = umap.find(keyToRemove);if (it != umap.end()) {umap.erase(it);}// 打印删除元素后的映射for (const auto& pair : umap) {std::cout << pair.first << ": " << pair.second << std::endl;}
}
  • 输出结果:

2: two
在这里插入图片描述

  1. 使用 emplace 插入元素
#include <iostream>
#include <unordered_map>int main() {std::unordered_map<int, std::string> umap;// 使用 emplace 插入元素auto result = umap.emplace(1, "one");if (result.second) {std::cout << "Insert successful." << std::endl;}// 尝试再次插入相同的键result = umap.emplace(1, "uno");if (!result.second) {std::cout << "Insert failed, key already exists." << std::endl;}// 打印映射for (const auto& pair : umap) {std::cout << pair.first << ": " << pair.second << std::endl;}
}
  • 输出结果:

Insert successful.
Insert failed, key already exists.
1: one
在这里插入图片描述

  1. 遍历 unordered_map
#include <iostream>
#include <unordered_map>int main() {std::unordered_map<int, std::string> umap = {{1, "one"},{2, "two"},{3, "three"}};// 使用范围 for 循环遍历for (const auto& pair : umap) {std::cout << pair.first << ": " << pair.second << std::endl;}// 使用迭代器遍历for (auto it = umap.begin(); it != umap.end(); ++it) {std::cout << it->first << ": " << it->second << std::endl;}
}
  • 输出结果:

1: one
2: two
3: three
1: one
2: two
3: three
在这里插入图片描述

  1. 使用 unordered_map 的 size 和 empty
#include <iostream>
#include <unordered_map>int main() {std::unordered_map<int, std::string> umap = {{1, "one"},{2, "two"}};std::cout << "Size of umap: " << umap.size() << std::endl;std::cout << "Is umap empty? " << (umap.empty() ? "Yes" : "No") << std::endl;
}
  • 输出结果:

Size of umap: 2
Is umap empty? No
在这里插入图片描述

unordered_set

  1. 基本使用
#include <iostream>
#include <unordered_set>int main() {std::unordered_set<int> uset;uset.insert(10);uset.insert(20);uset.insert(30);std::cout << "Size of uset: " << uset.size() << std::endl;
}
  • 输出结果:

Size of uset: 3
在这里插入图片描述

  1. 初始化列表
#include <iostream>
#include <unordered_set>int main() {std::unordered_set<int> uset = { 10, 20, 30, 40, 50 };for (int num : uset) {std::cout << num << " ";}std::cout << std::endl;
}
  • 输出结果:

50 10 20 30 40
在这里插入图片描述

  1. 检查元素是否存在
#include <iostream>
#include <unordered_set>int main() {std::unordered_set<int> uset = { 1, 2, 3 };int key = 2;if (uset.find(key) != uset.end()) {std::cout << "Element " << key << " exists in uset." << std::endl;}else {std::cout << "Element " << key << " does not exist in uset." << std::endl;}
}
  • 输出结果:

Element 2 exists in uset.
在这里插入图片描述

  1. 删除元素
#include <iostream>
#include <unordered_set>int main() {std::unordered_set<int> uset = { 1, 2, 3 };int keyToRemove = 2;if (uset.erase(keyToRemove)) {std::cout << "Element removed from uset." << std::endl;}else {std::cout << "Element not found in uset." << std::endl;}for (int num : uset) {std::cout << num << " ";}std::cout << std::endl;
}
  • 输出结果:

Element removed from uset.
1 3
在这里插入图片描述

  1. 使用 emplace 插入元素
#include <iostream>
#include <unordered_set>int main() {std::unordered_set<int> uset;// 使用 emplace 插入元素auto result = uset.emplace(10);if (result.second) {std::cout << "Insert successful." << std::endl;}// 尝试再次插入相同的元素result = uset.emplace(10);if (!result.second) {std::cout << "Insert failed, element already exists." << std::endl;}
}
  • 输出结果:

Insert successful.
Insert failed, element already exists.
在这里插入图片描述

  1. 遍历 unordered_set
#include <iostream>
#include <unordered_set>int main() {std::unordered_set<int> uset = { 5, 10, 15, 20, 25 };// 使用范围 for 循环遍历for (int num : uset) {std::cout << num << " ";}std::cout << std::endl;// 使用迭代器遍历for (auto it = uset.begin(); it != uset.end(); ++it) {std::cout << *it << " ";}std::cout << std::endl;
}
  • 输出结果:

5 10 15 20 25
5 10 15 20 25
在这里插入图片描述

  1. 使用 unordered_set 的 size 和 empty
#include <iostream>
#include <unordered_set>int main() {std::unordered_set<int> uset = { 1, 2, 3 };std::cout << "Size of uset: " << uset.size() << std::endl;std::cout << "Is uset empty? " << (uset.empty() ? "Yes" : "No") << std::endl;
}
  • 输出结果:

Size of uset: 3
Is uset empty? No
在这里插入图片描述

  1. 清空 unordered_set
#include <iostream>
#include <unordered_set>int main() {std::unordered_set<int> uset = { 1, 2, 3, 4, 5 };uset.clear(); // 清空 usetstd::cout << "Is uset empty after clear? " << (uset.empty() ? "Yes" : "No") << std::endl;
}
  • 输出结果:

Is uset empty after clear? Yes
在这里插入图片描述

  1. 自定义哈希函数
#include <iostream>
#include <unordered_set>// 自定义哈希函数
struct custom_hash {std::size_t operator()(int x) const {return std::hash<int>()(x);}
};int main() {std::unordered_set<int, custom_hash> uset = { 1, 2, 3 };for (int num : uset) {std::cout << num << " ";}std::cout << std::endl;
}
  • 输出结果:

1 2 3
在这里插入图片描述

这篇关于c++中 unordered_map 与 unordered_set 用法指南的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1022902

相关文章

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(