寻找乐趣的时间复杂度

dyn*_*amo 1 time stl vector c++11 c++14

你能解释一下 STL C++ 向量中的 find 函数是如何工作的,它的时间复杂度是多少?

vector<int> v;

if(find(v.begin(),v.end(),element)==v.end())
do this;
else 
do this
Run Code Online (Sandbox Code Playgroud)

Adr*_*n W 5

看看https://en.cppreference.com/w/cpp/algorithm/find

在“可能的实现”部分,您可以了解它是如何工作的。细节取决于具体的实现,可能会有所不同。在任何情况下std::find()都必须按顺序遍历您的集合,这决定了时间复杂度。即 O(n)。