简体   繁体   English

multiset,map和hash map复杂性

[英]multiset, map and hash map complexity

I would like to know the complexity in Big O notation of the STL multiset, map and hash map classes when: 在以下情况下,我想知道STL multiset,map和hash map类的Big O表示法的复杂性:

  • inserting entries 插入条目
  • accessing entries 访问条目
  • retrieving entries 检索条目
  • comparing entries 比较条目

map, set, multimap, and multiset map,set,multimap和multiset

These are implemented using a red-black tree , a type of balanced binary search tree . 这些是使用红黑树实现的 ,这是一种平衡的二叉搜索树 They have the following asymptotic run times: 它们具有以下渐近运行时间:

Insertion: O(log n) 插入:O(log n)
Lookup: O(log n) 查找:O(log n)
Deletion: O(log n) 删除:O(log n)

hash_map, hash_set, hash_multimap, and hash_multiset hash_map,hash_set,hash_multimap和hash_multiset

These are implemented using hash tables . 这些是使用哈希表实现的。 They have the following runtimes: 它们具有以下运行时:

Insertion: O(1) expected, O(n) worst case 插入:O(1)预期,O(n)最坏情况
Lookup: O(1) expected, O(n) worst case 查找:O(1)预期,O(n)最坏情况
Deletion: O(1) expected, O(n) worst case 删除:O(1)预期,O(n)最坏情况

If you use a proper hash function, you'll almost never see the worst case behavior, but it is something to keep in mind — see Denial of Service via Algorithmic Complexity Attacks by Crosby and Wallach for an example of that. 如果你使用正确的哈希函数,你几乎永远不会看到最坏的情况,但是要记住这一点 - 请参阅Crosby和Wallach的算法复杂性攻击中拒绝服务

You can find this information in the SGI STL documentation: http://www.sgi.com/tech/stl/ 您可以在SGI STL文档中找到此信息: http//www.sgi.com/tech/stl/

Basically, both multiset and maps are sorted binary trees, so inserting/finding 1 out of N entries takes O(log N). 基本上,multiset和maps都是排序的二叉树,因此在N个条目中插入/查找1需要O(log N)。 See Sorted Assoc. 请参阅Sorted Assoc。 Containers in the documentation. 文档中的容器。

Obviously, the big advantage of Hashmap is O(1) for inserting and finding entries. 显然,Hashmap的最大优点是O(1)用于插入和查找条目。

Accessing it after found is O(1) for all structures. 找到后访问它是所有结构的O(1)。 Comparison, what do you mean by that? 比较,你是什么意思? Sounds like O(1) to me, after all were found. 毕竟发现后听起来像O(1)。

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

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