C++面试八股文:知道std::unordered_set/std::unordered_map吗?
某日二师兄参加XXX科技公司的C++工程师开发岗位第27面:
面试官:知道
std::unordered_set/std::unordered_map吗?二师兄:知道。两者都是C++11引入的新容器,和
std::set和std::map功能类似,key唯一,unordered_map的value可变。二师兄:不同于
set/map,unordered_set/unordered_map都是无序容器。面试官:那你知道它们底层怎么实现的吗?
二师兄:两者底层使用哈希表实现,因此插入、删除和查找操作的平均时间复杂度为常数时间
O(1)。面试官:既然平均复杂度是
O(1),那么是不是可以取代set和map了?二师兄:这里是平均的时间复杂度。哈希表插入、删除和查找操作的最差时间复杂度是
O(n),要比set/map的O(log n)大。面试官:那你觉得哪些场景适合
set/map,哪些场景适合unordered_set/unordered_map?二师兄:
set/map适用于需要有序存储和快速查找的场景,而unordered_set/unordered_map适用于需要快速插入和查找的场景。面试官:
unordered_set/unordered_map对于key的类型有什么要求吗?二师兄:因为
unordered_set/unordered_map底层采用哈希表,所以在使用自定义类型作为key的时候,需要告诉编译器如何计算此类型的hash值,同时还要告诉编译器如何判断两个自定义类型的对象是否相等。以下代码无法通过编译:
#include <iostream>
#include <unordered_set>
struct Foo
{
std::string str;
int val;
};
int main(int argc, char const *argv[])
{
std::unordered_set<Foo> uset;
uset.insert({"42",42});
uset.insert({"1024",1024});
return 0;
}
二师兄:此时需要为
Foo类型实现bool operator==(const Foo& o) const函数和size_t operator()(const Foo& f) const仿函数,才能通过编译:
#include <iostream>
#include <unordered_set>
struct Foo
{
std::string str;
int val;
bool operator==(const Foo& o) const
{
return str == o.str && val == o.val;
}
};
struct Hash
{
size_t operator()(const Foo& f) const
{
return std::hash<std::string>()(f.str) ^ std::hash<int>()(f.val);
}
};
int main(int argc, char const *argv[])
{
std::unordered_set<Foo,Hash> uset;
uset.insert({"42",42});
uset.insert({"1024",1024});
return 0;
}
二师兄:当然我们也可以使用
std::function或者lambda来代替仿函数,目的都是为了使得编译器知道如何计算自定义类型的哈希值。面试官:用过
unordered_multiset/unordered_multimap吗?二师兄:没用过。但是应该和
multiset/multimap类似,只是底层也采用hash表实现。面试官:好的,今天的面试就结束了,请回去等消息吧。
对于今天面试官的表现,小伙伴们能给几分呢?不是面试官要放水,面完set/map之后再面unordered_set/unordered_map,真的没有那么多好问题,因为两者太像了。。。
好了,今天的面试到这里就结束了,让我们期待明天面试官的表现吧~
关注我,带你21天“精通”C++!(狗头)
C++面试八股文:知道std::unordered_set/std::unordered_map吗?的更多相关文章
- std::hash<std::pair<int, int> >
标题是搞笑的 ! 这个问题只需要 since C++11 问题:怎么让 unordered_map 支持使用 pair 作为 key? 如果你能把两个东西压到一个基本类型里那么就不用解决这个问题了 . ...
- c++ std::unordered_set
std::unordered_set template < class Key, // unordered_set::key_type/value_type class Hash = hash& ...
- E0443类模板 "std::unordered_set" 的参数太多
1>------ 已启动全部重新生成: 项目: QtGuiApplication20190416, 配置: Debug x64 ------1>Uic'ing QtGuiApplicati ...
- C++ folly库解读(三)Synchronized —— 比std::lock_guard/std::unique_lock更易用、功能更强大的同步机制
目录 传统同步方案的缺点 folly/Synchronized.h 简单使用 Synchronized的模板参数 withLock()/withRLock()/withWLock() -- 更易用的加 ...
- std::unique_lock<std::mutex> or std::lock_guard<std::mutex> C++11 区别
http://stackoverflow.com/questions/20516773/stdunique-lockstdmutex-or-stdlock-guardstdmutex The diff ...
- hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏
huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...
- 单独删除std::vector <std::vector<string> > 的所有元素
下面为测试代码: 1.创建 std::vector< std::vector<string> > vc2; 2.初始化 std::vector<string> vc ...
- C++11 std::bind std::function 高级使用方法
从最基础的了解,std::bind和std::function /* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ # ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- 没有与这些操作数匹配的 "<<" 运算符 操作数类型为: std::ostream << std::string
错误显示:没有与这些操作数匹配的 "<<" 运算符 操作数类型为: std::ostream << std::string 错误改正:要在头文 ...
随机推荐
- kubernetes核心实战(五)--- StatefulSets
7.StatefulSets StatefulSet 是用来管理有状态应用的工作负载 API 对象. StatefulSet 用来管理 Deployment 和扩展一组 Pod,并且能为这些 Pod ...
- LLaMA:开放和高效的基础语言模型
LLaMA:开放和高效的基础语言模型 论文:https://arxiv.org/pdf/2302.13971.pdf 代码:https://github.com/facebookresearch/ll ...
- sync.Pool:提高Go语言程序性能的关键一步
1. 简介 本文将介绍 Go 语言中的 sync.Pool并发原语,包括sync.Pool的基本使用方法.使用注意事项等的内容.能够更好得使用sync.Pool来减少对象的重复创建,最大限度实现对象的 ...
- React+Antd在使用form表单提交DatePicker日期框的时候会出现少八小时的情况
在使用antd做form表单提交的时候,突然发现了一个很有意思的bug.就是在使用datepicker组件日期框的时候会出现提交后少一天的问题 我在网上搜索了许多解决办法,也是困扰了我一天的时间,下面 ...
- vue表单绑定v-model
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 基于【ESLint+JavaScript Standard Style】标准的VUE/JS/html风格指南
小仙男前端代码风格规范指南v1.0 概述 本规范是适用于小仙男团队及前端团队所搭建的各种前端框架代码的通用风格规范指南: 使用时,请遵循指南细则进行代码风格约束,并在提交之前确保进行代码风格的修正操作 ...
- 回车,换行,转义字符“\r”,“\n”是什么关系?
1."回车"这个名词的来历. 关于"回车键"的来历,还得从机械英文打字机说起.在机械英文打字机上,有一个部件叫"字车",每打一个字符(原为单 ...
- 从零开始TP6配置ThinkPHP-ApiDoc
系统:windows11 集成环境:小皮(原phpstudy) composer:2.5 准备工作:安装小皮后,在软件管理中安装composer,2.3安装不上去,只能安装1.8.5,没关系安装后升级 ...
- C# 强行关闭其他软件对文件的占用
using System.Diagnostics; // 获取占用文件的进程并强制结束 public void CloseProcessByFileName(string fileName) { Pr ...
- STM32 + RTThread + UGUI
一.概述 开发板:STM32F103C8T6 显示器:ST7735S RT-Thread:5.0.0 玩过 GUI 的小伙伴都知道,界面的显示是一个个像素点组合起来的,那么直接构建出来炫酷的 GUI ...