关联容器(set,map,multiset和multimap)的查找时间是如何对数的?

时间:2018-03-13 06:48:46

标签: c++

使用二进制搜索来查找元素

排序数组:16个元素

1: Compare the key with 8th element  of sorted array.
2: Since memory is contiguous so reaching to the 8th element of sorted array is constant . 
3: Searching of key will continue by making the array half for each iteration  until key is found(or not found) So lookup time in sorted array for any key is logarithmic.

关联容器:16个元素。我假设,二进制搜索用于查找元素。

1: Compare the key with 8th element  of associative container.
2: Since memory is not contiguous ( node based) , so to find the 8th element , need to iterate this container linearly . 

1:关联容器的查找时间是如何对数的?关联容器中元素的唯一比较是对数的吗?

我有另一个问题:要找到一个元素,排序数组总是比任何关联容器提供更好的性能?二进制搜索只为随机访问迭代器提供保证的对数查找时间?    感谢

0 个答案:

没有答案