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

推荐订阅源

SecWiki News
SecWiki News
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Help Net Security
Attack and Defense Labs
Attack and Defense Labs
O
OpenAI News
博客园 - 聂微东
Y
Y Combinator Blog
N
News | PayPal Newsroom
IT之家
IT之家
C
Cybersecurity and Infrastructure Security Agency CISA
Engineering at Meta
Engineering at Meta
L
LangChain Blog
L
Lohrmann on Cybersecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog
C
CERT Recently Published Vulnerability Notes
爱范儿
爱范儿
P
Palo Alto Networks Blog
T
Threat Research - Cisco Blogs
N
News and Events Feed by Topic
G
Google Developers Blog
PCI Perspectives
PCI Perspectives
The Register - Security
The Register - Security
H
Heimdal Security Blog
V
Visual Studio Blog
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
TaoSecurity Blog
TaoSecurity Blog
博客园 - Franky
T
The Blog of Author Tim Ferriss
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
The Hacker News
The Hacker News
U
Unit 42
Security Latest
Security Latest
The GitHub Blog
The GitHub Blog

核桃的炼金工坊

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)$

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