惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Scott Helme
Scott Helme
S
Securelist
美团技术团队
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
W
WeLiveSecurity
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
AI
AI
L
Lohrmann on Cybersecurity
S
Security Affairs
Cloudbric
Cloudbric
SecWiki News
SecWiki News
爱范儿
爱范儿
雷峰网
雷峰网
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
N
News and Events Feed by Topic
I
InfoQ
S
Secure Thoughts
AWS News Blog
AWS News Blog
A
About on SuperTechFans
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
The Last Watchdog
The Last Watchdog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Check Point Blog
P
Palo Alto Networks Blog
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News
Latest news
Latest news
I
Intezer
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
D
Docker

核桃的炼金工坊

2023 韩国游记 Deducing This Stateful Metaprogramming 推し、燃ゆ Customization Point Object 2020 总结 搞了个 C++ 构建系统 软件设计哲学(NOTE) Paxos Note 关于 cpp 可见性的黑魔法后门 一个关于 private member function detect 的 SFINAE 模板 User-defined conversion and Copy elision VIM and Latex Compare Between CRTP and Virtual Interface in C++ Compile Time Reflection in C++11 C++11内存模型 在C++17中的部分新特性 Const Reference of Pointer
C++23: Flat Containers
Hawtian Wang · 2023-09-21 · via 核桃的炼金工坊

C++23 的周期里又补充了四个非常重要的容器,这应该是继 C++11 加入 unordered_{map|set} 以来, 终于再次向 STL 里增加的容器。这四个容器是其实就是 map/set/multimap/multisetflat 版本。

  • flat_map
  • flat_set
  • flat_multimap
  • flat_multiset

这组新的容器可以在合适的场景下替换其对应的容器(当然也包括了他们的 unordered_ 的版本), 和同功能的其他容器相比:

  • 更慢的插入和删除,因为插入和删除都会导致元素的移动,像 vectorinsert 一样
  • 插入和删除都会造成旧迭代器的失效,因为不论是插入还是删除,都会伴随着大部分元素的移动,保存的旧迭代器很难不失效
  • 元素类型必须是可移动(moveable)或者可拷贝的(copyable)
  • 异常安全变得更弱了,像 vector 一样构造函数的异常没法保证后面的元素可以成功构造

那上面付出的代价,我们获得了什么?

  • 更快的遍历,在一个简单数组上的一次后继的时间复杂度是 $O(1)$ 而对于 map 这个时间是$O(log_2n)$
  • 得益于 vector 的实现,现在我们的迭代器是 random access 的了
  • 更小的空间复杂度,对于 map 不用保存节点的额外信息了,对于 unordered_map 不会有空的 slot
  • 更好的缓存性能(因为存储是连续的)
  • 更快的查找,虽然数量级上都还是 $O(log_2n)$ 但是常数上小了很多
ContainerOrderedInsertEraseFinditer++
map/setYes$O(log_2n)$$O(log_2n)$$O(log_2n)$$O(log_2n)$
unordered_{map/set}No$O(1)$$O(1)$$O(1)$$O(1)$
flat_{map/set}Yes$O(n)$$O(n)$$O(log_2n)$, but smaller constant$O(1)$

所以它应该更适合存那些构造出来基本就不会改的数据,获得更好的遍历和查找性能。