简体   繁体   English

分析 std::map - 为什么使用 map /hashset/hastable 由于空间复杂性被认为不好使用?

[英]Analysing std::map - Why is using a map /hashset/hastable considered bad to use due to space complexity?

I have always heard that it is better to avoid using hash table/maps since they have large space complexity.我一直听说最好避免使用 hash 表/地图,因为它们的空间复杂度很大。 How is the space complexity of maps different from that of a vector or an array of N elements?地图的空间复杂度与向量或 N 元素数组的空间复杂度有何不同? What would be an alternative to using a map?使用 map 的替代方法是什么? Would it be equally bad to use a map or a vector in terms of space complexity when number of elements is large?当元素数量很大时,在空间复杂度方面使用 map 或向量是否同样糟糕? Why?为什么?

The guideline to prefer vector or array bases on the assumption, that the amount of items in a map is mostly very small (few KB).首选向量或数组的准则基于这样的假设,即 map 中的项目数量大多非常小(几 KB)。 Using a map, the key has to be stored additionally.使用 map,必须额外存储密钥。 Then a verctor is smaller in memory and therfore more heap friendly.然后在 memory 中的 verctor 更小,因此对堆更友好。 Using the heap memory efficiently can decrease the runtime a lot.有效地使用堆 memory 可以大大减少运行时间。

Reference: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon2-prefer-using-stl-vector-by-default-unless-you-have-a-reason-to-use-a-different-container参考: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon2-prefer-using-stl-vector-by-default-unless-you-have-a-reason-to-use-a-different-con

You also have to keep in mind the rules of the O-Natation.您还必须牢记 O-Natation 的规则。 We only considere large large N-values.我们只考虑大的 N 值。 Eg the term O(30N) is reduced to O(N).例如,O(30N) 项被简化为 O(N)。 The 30 can be neglected for large large N-values.对于较大的 N 值,可以忽略 30。 But if you use low N-values, than the "real" complexity is more important than the convergence-value.但是,如果您使用低 N 值,则“真实”复杂性比收敛值更重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM