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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4604136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:53:56  来源:igfitidea点击:

How to search for an element in an stl list?

c++stl

提问by Prasanth Madhavan

Is there a find()function for list as there was in vector?

有没有find()像 vector 中那样的 list 函数?

Is there a way to do that in list?

有没有办法在列表中做到这一点?

回答by wkl

You use std::findfrom <algorithm>, which works equally well for std::listand std::vector. std::vectordoes not have its own search/find function.

您使用std::findfrom <algorithm>,它同样适用于std::liststd::vectorstd::vector没有自己的搜索/查找功能。

#include <list>
#include <algorithm>

int main()
{
    std::list<int> ilist;
    ilist.push_back(1);
    ilist.push_back(2);
    ilist.push_back(3);

    std::list<int>::iterator findIter = std::find(ilist.begin(), ilist.end(), 1);
}

Note that this works for built-in types like intas well as standard library types like std::stringby default because they have operator==provided for them. If you are using using std::findon a container of a user-defined type, you should overload operator==to allow std::findto work properly: EqualityComparableconcept

请注意,这适用于内置类型int以及std::string默认情况下的标准库类型,因为它们已operator==为它们提供。如果您std::find在用户定义类型的容器上使用 using ,您应该重载operator==以允许std::find正常工作:EqualityComparableconcept

回答by Jan Holecek

No, not directly in the std::list template itself. You can however use std::find algorithm like that:

不,不是直接在 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()

回答by B?ови?

Besides using std::find (from algorithm), you can also use std::find_if(which is IMO better then std::find), or other find algorithm from this list

除了使用 std::find (来自算法),您还可以使用std::find_if(IMO 比 std::find 更好)或此列表中的其他查找算法



#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;
    }
}

回答by CashCow

What you can do and what you should do are different matters.

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

If the list is very short, or you are only ever going to call find once then use the linear approach above.

如果列表很短,或者您只会调用 find 一次,则使用上面的线性方法。

However linear-search is one of the biggest evils I find in slow code, and consider using an ordered collection (set or multiset if you allow duplicates). If you need to keep a list for other reasons eg using an LRU technique or you need to maintain the insertion order or some other order, create an index for it. You can actually do that using a std::set of the list iterators (or multiset) although you need to maintain this any time your list is modified.

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

回答by Praveen Kumar

No, find() method is not a member of std::list. Instead, use std::findfrom <algorithm>

不, find() 方法不是std::list. 相反,使用std::findfrom<algorithm>

    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;