C++ unordered_set基于哈希表实现,提供平均O(1)的插入、查找和删除操作,不保证元素顺序。它使用哈希函数将元素映射到桶中,采用链地址法解决冲突,默认使用std::hash,支持自定义哈希函数。当负载因子超过阈值(默认1.0)时触发rehash,可通过reserve预分配空间优化性能。相比set的O(log n)操作和有序存储,unordered_set更适合无需排序且追求高效存取的场景。

C++
unordered_set
哈希集合实现
unordered_set
基本操作:
立即学习“C++免费学习笔记(深入)”;
unordered_set
哈希冲突:
由于哈希函数可能将不同的元素映射到同一个桶中,因此哈希冲突是不可避免的。
unordered_set
unordered_set
unordered_set
代码示例:
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> mySet;
// 插入元素
mySet.insert(10);
mySet.insert(20);
mySet.insert(30);
mySet.insert(20); // 插入重复元素,不会生效
// 查找元素
if (mySet.find(20) != mySet.end()) {
std::cout << "20 found in the set" << std::endl;
} else {
std::cout << "20 not found in the set" << std::endl;
}
// 删除元素
mySet.erase(20);
// 再次查找元素
if (mySet.find(20) != mySet.end()) {
std::cout << "20 found in the set" << std::endl;
} else {
std::cout << "20 not found in the set" << std::endl;
}
// 遍历集合
std::cout << "Set elements: ";
for (const int& element : mySet) {
std::cout << element << " ";
}
std::cout << std::endl;
return 0;
}unordered_set
unordered_set
unordered_set
set
set
unordered_set
set
C++ unordered_set 的哈希函数是如何工作的?
unordered_set
unordered_set
std::hash
std::hash
int
float
string
对于自定义类型,你需要提供自己的哈希函数。这可以通过以下两种方式实现:
std::hash
std::hash
operator()
unordered_set
示例:自定义类型的哈希函数
#include <iostream>
#include <unordered_set>
struct MyStruct {
int x;
int y;
bool operator==(const MyStruct& other) const {
return x == other.x && y == other.y;
}
};
// 方法 1: 重载 std::hash 模板
namespace std {
template <>
struct hash<MyStruct> {
size_t operator()(const MyStruct& s) const {
// 一个简单的哈希函数,可以将 x 和 y 的值组合起来
return ((hash<int>()(s.x) ^ (hash<int>()(s.y) << 1)) >> 1);
}
};
}
// 方法 2: 提供自定义的哈希函数对象
struct MyStructHash {
size_t operator()(const MyStruct& s) const {
return ((std::hash<int>()(s.x) ^ (std::hash<int>()(s.y) << 1)) >> 1);
}
};
int main() {
// 使用重载的 std::hash
std::unordered_set<MyStruct> mySet1;
mySet1.insert({1, 2});
mySet1.insert({3, 4});
// 使用自定义的哈希函数对象
std::unordered_set<MyStruct, MyStructHash> mySet2;
mySet2.insert({5, 6});
mySet2.insert({7, 8});
return 0;
}哈希函数的设计原则:
选择合适的哈希函数对于
unordered_set
unordered_set
C++ unordered_set 何时进行 rehash?
unordered_set
unordered_set
unordered_set
unordered_set
max_load_factor()
rehash()
rehash()
unordered_set
unordered_set
Rehash 的过程:
Rehash 的过程包括以下步骤:
unordered_set
Rehash 是一个耗时的操作,因为它需要遍历
unordered_set
示例:控制 rehash
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> mySet;
// 设置最大负载因子
mySet.max_load_factor(0.5);
// 预留足够的空间,以减少 rehash 的次数
mySet.reserve(1000);
// 插入大量元素
for (int i = 0; i < 1000; ++i) {
mySet.insert(i);
}
std::cout << "Load factor: " << mySet.load_factor() << std::endl;
std::cout << "Bucket count: " << mySet.bucket_count() << std::endl;
return 0;
}通过设置最大负载因子和预留足够的空间,可以有效地控制
unordered_set
C++ unordered_set 和 set 的区别是什么?
unordered_set
set
底层实现:
unordered_set
set
元素顺序:
unordered_set
set
<
性能:
| 操作 | @@######@@ (平均) | @@######@@ (平均) |
|---|---|---|
| 插入 (insert) | O(1) | O(log n) |
| 查找 (find) | O(1) | O(log n) |
| 删除 (erase) | O(1) | O(log n) |
unordered_set
set
内存占用:
unordered_set
set
unordered_set
使用场景:
set
set
总结:
| 特性 | @@######@@ | @@######@@ |
|---|---|---|
| 底层实现 | 哈希表 | 红黑树 |
| 元素顺序 | 无序 | 有序 |
| 查找、插入、删除 | O(1) (平均) | O(log n) |
| 内存占用 | 较高 | 较低 |
选择
unordered_set
set
unordered_set
set
unordered_set
set
unordered_set
set
以上就是C++ unordered_set使用 哈希集合实现的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号