gpt4 book ai didi

c++ - 我什么时候应该使用 unordered_map 而不是 std::map

转载 作者:IT老高 更新时间:2023-10-28 23:14:19 24 4
gpt4 key购买 nike

我想知道在哪种情况下我应该使用 unordered_map 而不是 std::map。

每次我不注意 map 中元素的顺序时,我都必须使用 unorderd_map ?

最佳答案

map

  1. 通常使用 red-black tree 实现.
  2. 元素已排序。
  3. 内存使用量相对较小(哈希表不需要额外的内存)。
  4. 相对快速的查找:O(log N)。

unordered_map

  1. 通常使用 hash-table 实现.
  2. 元素未排序。
  3. 需要额外的内存来保存哈希表。
  4. 快速查找 O(1),但恒定时间取决于 hash-function这可能相对较慢。另请记住,您可以与 Birthday problem 会面。 .

关于c++ - 我什么时候应该使用 unordered_map 而不是 std::map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6173860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com