[英]How to search for an element in an stl list?
Is there a find()
function for list as there was in vector?列表中是否有
find()
函数,就像向量中那样?
Is there a way to do that in list?有没有办法在列表中做到这一点?
You use std::find
from <algorithm>
, which works equally well for std::list
and std::vector
.您使用
<algorithm>
std::find
,它同样适用于std::list
和std::vector
。 std::vector
does not have its own search/find function. std::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 int
as well as standard library types like std::string
by default because they have operator==
provided for them.请注意,默认情况下这适用于像
int
这样的内置类型以及像std::string
这样的标准库类型,因为它们为它们提供了operator==
。 If you are using using std::find
on a container of a user-defined type, you should overload operator==
to allow std::find
to work properly: EqualityComparable
concept如果您在用户定义类型的容器上使用
std::find
,您应该重载operator==
以允许std::find
正常工作: EqualityComparable
概念
No, not directly in the std::list
template itself.不,不是直接在
std::list
模板本身中。 You can however use std::find
algorithm like that:但是,您可以像这样使用
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()
Besides using std::find
(from algorithm), you can also use std::find_if
(which is, IMO, better than 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;
}
}
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.
如果您出于其他原因需要保留列表,例如使用 LRU 技术,或者您需要维护插入顺序或其他顺序,请为其创建索引。 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.
您实际上可以使用列表迭代器(或多重集)的 std::set 来做到这一点,尽管您需要在修改列表时维护它。
No, find() method is not a member of std::list
.不, find() 方法不是
std::list
的成员。 Instead, use std::find
from <algorithm>
相反,使用
std::find
from <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;
What if my list is list < int > l{1,2,3,4,3}; 如果我的列表是列表<int> l {1,2,3,4,3},该怎么办?
and i need to return iterator to the first element with "value = 3" How works function std:find, it returns the first existence of value? 并且我需要将迭代器返回到“值= 3”的第一个元素。std:find函数的工作方式是什么,它返回值的第一个存在?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.