某日二师兄参加XXX科技公司的C++工程师开发岗位第27面:

面试官:知道std::unordered_set/std::unordered_map吗?

二师兄:知道。两者都是C++11引入的新容器,和std::setstd::map功能类似,key唯一,unordered_mapvalue可变。

二师兄:不同于set/mapunordered_set/unordered_map都是无序容器。

面试官:那你知道它们底层怎么实现的吗?

二师兄:两者底层使用哈希表实现,因此插入、删除和查找操作的平均时间复杂度为常数时间O(1)

面试官:既然平均复杂度是O(1),那么是不是可以取代setmap了?

二师兄:这里是平均的时间复杂度。哈希表插入、删除和查找操作的最差时间复杂度是O(n),要比set/mapO(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吗?的更多相关文章

  1. std::hash<std::pair<int, int> >

    标题是搞笑的 ! 这个问题只需要 since C++11 问题:怎么让 unordered_map 支持使用 pair 作为 key? 如果你能把两个东西压到一个基本类型里那么就不用解决这个问题了 . ...

  2. c++ std::unordered_set

    std::unordered_set template < class Key, // unordered_set::key_type/value_type class Hash = hash& ...

  3. E0443类模板 "std::unordered_set" 的参数太多

    1>------ 已启动全部重新生成: 项目: QtGuiApplication20190416, 配置: Debug x64 ------1>Uic'ing QtGuiApplicati ...

  4. C++ folly库解读(三)Synchronized —— 比std::lock_guard/std::unique_lock更易用、功能更强大的同步机制

    目录 传统同步方案的缺点 folly/Synchronized.h 简单使用 Synchronized的模板参数 withLock()/withRLock()/withWLock() -- 更易用的加 ...

  5. 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 ...

  6. 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 ...

  7. 单独删除std::vector <std::vector<string> > 的所有元素

    下面为测试代码: 1.创建 std::vector< std::vector<string> > vc2; 2.初始化 std::vector<string> vc ...

  8. C++11 std::bind std::function 高级使用方法

    从最基础的了解,std::bind和std::function /* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ # ...

  9. 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++> 给出排序好的 ...

  10. 没有与这些操作数匹配的 "<<" 运算符 操作数类型为: std::ostream << std::string

    错误显示:没有与这些操作数匹配的 "<<" 运算符       操作数类型为:  std::ostream << std::string 错误改正:要在头文 ...

随机推荐

  1. [Linux]Xmanager+Xshell远程管理桌面版CentOS物理服务器的桌面版CentOS虚拟机

    1 需求/背景 在项目现场有这么一个情况,有1台Gnome版的CentOS的物理服务器,其内运行了2台通过vmware安装的Gnome桌面版的CentOS的虚拟服务器. 按照常规做法是: 将唯一的1台 ...

  2. [Linux]Linux中安装软件的方式?

    近日处理安全漏洞时,出现了这样一个问题: 判断某软件组件是通过何种方式安装的. 知道是何种方式安装,才方便做进一步的解决(升级/配置/卸载等操作) 1 解压即用 例如: sublime_text.py ...

  3. [Windows/Linux]判别服务器: 虚拟机 | 物理机 ?

    物理主机,一般称: [宿主机] 虚拟机信息,一般涉及如下关键词: VMware : VMware 虚拟化技术 Vistualbox KVM(Kernel-based Virtual Machine): ...

  4. [IDE]IDEA build artifacts过程很慢的解决方案[转载]

    解决方案 可能1 可能是缓存的文件太多了导致: File->Invalidate Caches /Restart,清理缓存, 并重启IDEA.重启之后,会重建索引, 此过程较慢, 但build的 ...

  5. sql lag函数

    lag https://spark.apache.org/docs/latest/api/sql/#lag lag(input[, offset[, default]]) OVER (PARTITIO ...

  6. 在有限 computational budget 下,借助 low-fidelity 模型提高精度

    论文名称:context-aware learning of hierarchies of low-fidelity models for multi-fidelity uncertainty qua ...

  7. 重复delete 对象指针后的 异常调用栈怪异 解析

    Release版VC6 MFC程序 程序正常退出时得到一个如下异常调用栈: 0:000> kb # ChildEBP RetAddr Args to Child WARNING: Frame I ...

  8. 【FAQ】关于JavaScript版本的华为地图服务Map的点击事件与Marker的点击事件存在冲突的解决方案

    一. 问题描述 创建地图对象,并添加marker标记,对map和marker均添加了点击事件: <body> <script> function initMap() { // ...

  9. CSS 点击穿透pointer-events

    在项目中,当需要展示一个元素在最顶层,但又不想让它影响下层的交互,可以pointer-events:none pointer-events介绍 pointer-events: auto | none ...

  10. Grafana 系列-统一展示-2-Prometheus 数据源

    系列文章 Grafana 系列文章 Grafana Prometheus 数据源 Grafana 提供了对 Prometheus 的内置支持.本文会介绍 Grafana Prometheus(也包括 ...