C++ 如何在stl列表中搜索元素?

C++ 如何在stl列表中搜索元素?,c++,stl,C++,Stl,列表是否有一个与vector中相同的find()函数 在列表中有这样做的方法吗?您可以从中使用否,而不是直接在std::list模板本身中使用。但是,您可以使用std::find这样的算法: std::list<int> my_list; //... int some_value = 12; std::list<int>::iterator iter = std::find (my_list.begin(), my_list.end(), some_value); //

列表是否有一个与vector中相同的
find()
函数


在列表中有这样做的方法吗?

您可以从
中使用否,而不是直接在
std::list
模板本身中使用。但是,您可以使用
std::find
这样的算法:

std::list<int> my_list;
//...
int some_value = 12;
std::list<int>::iterator iter = std::find (my_list.begin(), my_list.end(), some_value);
// now variable iter either represents valid iterator pointing to the found element,
// or it will be equal to my_list.end()
std::列出我的清单;
//...
int一些_值=12;
std::list::iterator iter=std::find(my_list.begin()、my_list.end()、some_值);
//现在变量iter表示指向找到的元素的有效迭代器,
//或者它将等于我的_list.end()

你能做什么和应该做什么是不同的事情

如果列表很短,或者您只需要调用find一次,那么使用上面的线性方法


<>但是,线性搜索是我在慢代码中发现的最大缺点之一,并且考虑使用有序集合(如果允许重复的话,集合或多集)。如果出于其他原因(如使用LRU技术)需要保留列表,或者需要维护插入顺序或其他顺序,请为其创建索引。实际上,您可以使用列表迭代器的std::集(或多集)来实现这一点,尽管您需要在修改列表时随时维护它。

除了使用
std::find
(从算法)之外,您还可以使用(在我看来,它比std::find更好)或其他从


#包括
#包括
#包括
int main()
{
std::list myList{5,19,34,3,33};
auto it=std::find_if(std::begin(myList)),
标准::结束(myList),
[&](常量int v){返回0==(v%17);};
if(myList.end()==it)
{
std::cout否,find()方法不是
std::list
的成员。 相反,使用
std::find
from

std::listl;
std::list::迭代器位置;
l、 推回(1);
l、 推回(2);
l、 推回(3);
l、 推回(4);
l、 推回(5);
l、 推回(6);
int元素=3;
pos=查找(l.开始(),l.结束(),元素);
如果(位置!=l.end())

std::cout
std::vector
有一个
find()
方法?这对我来说是个新闻。mybad…我是说fint(vectoritator.begin(),vectoritator.end(),string)你是对的,
std::vector
没有
find()
方法。@Marc:二进制搜索需要随机访问,列表不允许(我们在这里讨论的是STL列表,所以它是一个链表,而不是类似ArrayList的东西)@马克:事实上,对于复杂的
运算符来说尤其值得。为什么find_if更好?如果你在搜索一个与相等匹配的元素,你是说你不会使用find,而是会使用find_if吗?find_if确实允许你自定义搜索,但是如果你必须使用一些可怕的函子来搜索相等的元素,那么它会与使用find相比,ke代码看起来非常难看。@CashCow这完全取决于向量或列表中的元素类型。有时无法使用std::find。是的,如果这就是为什么它是库的一部分,有时需要find_,但这并不能使它“更好”这样,即使std::find有效,您也可以使用它。+1如果您已经有一元谓词,find_if更好。也许您应该添加一个小示例来展示它的优势。
std::find()
的时间复杂度是多少?也许,O(n)?对于已排序的容器没有二进制搜索功能?惰性实现。@LDS-
std::binary_search
提供二进制搜索。string具有find series函数。不确定其他容器是否具有。std::binary_search
仅返回
true
false
。我认为OP可能对
LB=std>感兴趣::下限(v.begin(),v.end(),value)
。然后
LB
是我们寻找的第一个元素的迭代器>=
value
。如果
*LB==value
,则
LB-v.begin()
值的索引。
std::list<int> my_list;
//...
int some_value = 12;
std::list<int>::iterator iter = std::find (my_list.begin(), my_list.end(), some_value);
// now variable iter either represents valid iterator pointing to the found element,
// or it will be equal to my_list.end()
#include <list>
#include <algorithm>
#include <iostream>

int main()
{
    std::list<int> myList{ 5, 19, 34, 3, 33 };
    

    auto it = std::find_if( std::begin( myList ),
                            std::end( myList ),
                            [&]( const int v ){ return 0 == ( v % 17 ); } );
        
    if ( myList.end() == it )
    {
        std::cout << "item not found" << std::endl;
    }
    else
    {
        const int pos = std::distance( myList.begin(), it ) + 1;
        std::cout << "item divisible by 17 found at position " << pos << std::endl;
    }
}
    std :: list < int > l;
    std :: list < int > :: iterator pos;

    l.push_back(1);
    l.push_back(2);
    l.push_back(3);
    l.push_back(4);
    l.push_back(5);
    l.push_back(6);

    int elem = 3;   
    pos = find(l.begin() , l.end() , elem);
    if(pos != l.end() )
        std :: cout << "Element is present. "<<std :: endl;
    else
        std :: cout << "Element is not present. "<<std :: endl;