c++中如何使用std::unordered_set实现快速查找_c++集合用法【实例】
技术百科
冰火之心
发布时间:2026-01-17
浏览: 次 std::unordered_set查找快因底层哈希表,平均O(1);自定义类型须特化std::hash并重载==;insert返回pair,find返回iterator;需用reserve/rehash预分配桶避免rehash卡顿。
std::unordered_set 查找为什么快
因为底层是哈希表,平均时间复杂度 O(1);不像 std::set 那样用红黑树、要 O(log n)。但注意:最坏情况(大量哈希冲突)会退化到 O(n),所以别随便用自定义类型又不写好 std::hash 特化。
插入和查找的基本写法
直接用 insert() 和 find,返回值类型不同,容易混淆:
-
insert()返回std::pair,second是是否新插入 -
find()返回iterator,查不到时等于end() - 不能用
operator[]——unordered_set没下标访问
std::unordered_sets; s.insert(42); // OK s.insert(42); // 无效果,返回 {已有迭代器, false} auto it = s.find(42); // it != s.end() 表示找到了 if (it != s.end()) { std::cout << *it << "\n"; // 输出 42 }
自定义类型必须提供 hash 和 equal_to
否则编译报错,典型错误信息:error: call to implicitly-deleted default constructor of 'std::hash。两个条件缺一不可:
- 特化
std::hash,重载operator()返回size_t - 提供等价判断(默认用
operator==,也可传入第 3 个模板参数EqualKey)
struct Point {
int x, y;
bool operator==(const Point& other) const {
return x == other.x && y == other.y;
}
};
namespace std {
template<>
struct hash {
size_t operator()(const Point& p) const {
return hash{}(p.x) ^ (hash{}(p.y) << 1);
}
};
}
std::unordered_set pts;
pts.insert({1, 2});
性能陷阱:rehash 和 bucket_count
插入导致负载因子超限会触发 rehash,所有元素重新散列,瞬间卡顿。可提前预留空间避免:
- 用
reserve(n)预分配至少能存n个元素的桶数(不是直接设bucket_count) -
max_load_factor()默认是1.0,设太小会频繁 rehash,太大则冲突增多 - 调用
rehash()手动扩桶时,传入的是桶数量(bucket_count),不是元素数
如果事先知道大概有 1000 个元素,建议:s.reserve(1024) 或 s.rehash(1024),而不是 s.reserve(1000) —— 因为内部桶数通常是 2 的幂。
# ai
# 的是
# 不像
# 又不
# 已有
# 自定义
# 也可
# 特化
# default
# Error
# c++
# 值类型
# 报错
# 为什么
# red
# operator
# 太大
# 不能用
# constructor
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- 如何用正则表达式精确匹配最多含一个换行符的起止片段
- Win11怎么关闭搜索历史_Win11清除任务栏搜
- Windows系统时间服务错误_W32Time服务
- Win11怎么设置默认邮件应用_Windows11
- PHP 中如何在函数内持久化修改引用变量的指向
- Windows10系统怎么查看显卡型号_Win10
- Win11怎么设置开机问候语_自定义Win11锁屏
- php本地部署后session无法保存_sessi
- Windows10电脑怎么设置虚拟内存_Win10
- php转exe用什么工具打包快_高效打包软件推荐【
- PythonWeb前后端整合项目教程_FastAP
- Win10如何卸载预装Edge扩展_Win10卸载
- Win11快速助手怎么用_Win11远程协助连接教
- windows 10应用商店区域怎么改_windo
- Python 中将 ISO 8601 时间戳转换为
- Win11蓝牙开关不见了怎么办_Win11蓝牙驱动
- Win11怎么关闭任务栏小组件_Windows11
- Win11系统占用空间大怎么办 Win11深度瘦身
- Win11怎么设置开机自动连接宽带_Windows
- Win11怎么关闭右下角弹窗_Win11拦截系统通
- Win11怎么查看wifi信号强度_检测Windo
- Win11鼠标灵敏度怎么调 Win11鼠标指针移动
- Windows电脑键盘突然失灵怎么办?(驱动与硬件
- php错误怎么开启_display_errors与
- C++ static_cast和dynamic_c
- LINUX如何开放防火墙端口_Linux fire
- php485能和物联网模块通信吗_php485对接
- 企业SEO优化选择网站建设模板的技巧
- Python代码测试策略_质量保障解析【教程】
- Win11输入法选字框不见了怎么办_Win11输入
- Win11怎么关闭触控板_Win11笔记本禁用触摸
- Win11怎么关闭应用权限_Windows11相机
- c++如何获取map中所有的键_C++遍历键值对提
- c# Task.Yield 的作用是什么 它和Ta
- Mac如何彻底清理浏览器缓存?(Safari与Ch
- Win11任务栏颜色怎么改_Win11自定义任务栏
- php订单日志怎么记录物流_php记录订单物流变更
- Python网络异常模拟_测试说明【指导】
- Win11怎么设置虚拟内存最佳大小_Windows
- Python数据挖掘进阶教程_分类回归与聚类案例解
- php485在macos下怎么配置_php485
- Go 中 defer 语句在 goroutine
- C++如何使用std::transform批量处理
- php打包exe后无法读取环境变量_变量配置方法【
- php嵌入式多设备通信怎么实现_php同时管理多个
- Python对象比较与排序_集合使用说明【指导】
- php485函数怎么捕获异常_php485错误处理
- Win11怎么把图标拖到任务栏_Win11固定应用
- c++ nullptr与NULL区别_c++11空
- LINUX如何删除用户和用户组_Linux use

QQ客服