Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么';std::set的std::lower_界的时间复杂度是多少?_C++_Algorithm_Stl_Set_Complexity Theory - Fatal编程技术网

C++ 什么';std::set的std::lower_界的时间复杂度是多少?

C++ 什么';std::set的std::lower_界的时间复杂度是多少?,c++,algorithm,stl,set,complexity-theory,C++,Algorithm,Stl,Set,Complexity Theory,我知道有std::set::lower_bound,时间复杂度是O(log),我看到std::lower_bound在std::set上操作时比std::lower_bound慢得多 我在谷歌上搜索发现: 因此很明显,因为std::advance对于set::iterator是线性的,所以整个std::lower_bound占据了O(n) 然而,当我使用它时,它的运行速度比O(n)快得多(一些朋友也这么说),有人能解释为什么或者告诉我它不是那样的吗。在非随机访问迭代器上,std::lower

我知道有
std::set::lower_bound
,时间复杂度是
O(log)
,我看到
std::lower_bound
std::set
上操作时比
std::lower_bound
慢得多

我在谷歌上搜索发现:

因此很明显,因为
std::advance
对于
set::iterator
是线性的,所以整个
std::lower_bound
占据了
O(n)


然而,当我使用它时,它的运行速度比
O(n)
快得多(一些朋友也这么说),有人能解释为什么或者告诉我它不是那样的吗。

在非随机访问迭代器上,
std::lower_-bound()
的保证复杂性是
O(n)
。如果该算法检测到搜索在有序关联容器上,它可能会利用树结构实现更好的复杂性。我不知道是否有任何实现会这样做;DR:每当容器提供与现有算法同名的方法时,它都会这样做,因为内部实现速度更快(取决于容器的属性),因此您应该直接使用它


问题是复杂性是易变的:O(N)什么

非随机访问迭代器的复杂性保证为:

  • O(N)迭代
  • O(log2(N))比较
取决于迭代还是比较是瓶颈,这实际上改变了一切


理论上,在排序关联容器的情况下,人们可能希望专门化
std::lower_bound
,以利用数据已经排序的事实;然而,在实践中,这是相对困难的。主要的问题是,
集合
的比较谓词和传递给
下限
的比较谓词确实是一个相同的谓词,因此算法需要假设不是这样(除非另有证明)。由于该算法使用迭代器而不是范围/容器,因此证明不是这样留给读者作为练习。

根据25.4.3.1下限:复杂性:最多log2(最后− f第一)+O(1)比较。@DieterLücking:复杂性总是很难定义。。。Dietmar Kühl在这里介绍了总体复杂性,而您具体针对的是比较的数量。因此,你们都是对的:在最坏的情况下,迭代次数是O(N),比较次数是O(log2(N))。哪一个在运行时占主导地位取决于具体情况。