C++ 如何将 lambda 用于 std::find_if
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42933943/
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
How to use lambda for std::find_if
提问by Tim
I am trying to use std::find_if to find an object that matches some criteria. Consider the following:
我正在尝试使用 std::find_if 来查找符合某些条件的对象。考虑以下:
struct MyStruct
{
MyStruct(const int & id) : m_id(id) {}
int m_id;
};
...
std::vector<MyStruct> myVector; //... assume it contains things
MyStruct toFind(1);
std::vector<MyStruct>::iterator i = std::find_if(myVector.begin(), myVector.end(), ???);
I am not sure what to put in the ???
我不知道该放什么 ???
All the examples I have seen have a lambda that uses a hard-coded value to check for the ID. What I want is to return the iterator/success only if the id of toFind
matches the id of one of the items in the vector.
我见过的所有示例都有一个使用硬编码值来检查 ID 的 lambda。我想要的是仅当 idtoFind
与向量中项目之一的 id匹配时才返回迭代器/成功。
All the examples I have see don't show me how to pass the two parameters
我看到的所有例子都没有告诉我如何传递这两个参数
EDIT
编辑
Additional info There are two different scenarios I have to use this for One in which there is an == operator for the struct and another in which there is no operator == for the struct - and i can't create one because the criteria for finding a match for this scenario is not as rigid as would be used for an equivalence operator.
附加信息 有两种不同的场景我必须使用它,一种是结构有 == 运算符,另一种是结构没有运算符 == - 我无法创建一个,因为标准为这种情况寻找匹配并不像用于等价运算符那样严格。
(And thanks to all who responded; I was able to use find() in one case and with your help was able to use find_if() for the other)
(感谢所有回复的人;在一种情况下我能够使用 find() 并且在您的帮助下能够将 find_if() 用于另一种情况)
回答by Kerrek SB
Try this:
尝试这个:
std::find_if(
myVector.begin(), myVector.end(),
[&toFind](const MyStruct& x) { return x.m_id == toFind.m_id;});
Alternatively, if you had defined an appropriate ==
overload for MyStruct
, you could just use find
:
或者,如果您为 定义了适当的==
重载MyStruct
,则可以使用find
:
std::find(myVector.begin(), myVector.end(), toFind); // requires ==
The find_if
version is usually best when you have some kind of heterogeneouslookup, for example if you were just given an int
, not a value of MyStruct
.
find_if
当您进行某种异构查找时,该版本通常是最好的,例如,如果您只获得了int
,而不是 的值MyStruct
。
回答by NathanOliver
This is where the lambda capture comes into play. Besides saying what type of parameters are to be passed to the lambda you can also say what existing variables are to be used to construct the lambda with. So in this case you would have something like
这就是 lambda 捕获发挥作用的地方。除了说明要传递给 lambda 的参数类型之外,您还可以说明要使用哪些现有变量来构造 lambda。所以在这种情况下,你会有类似的东西
std::vector<MyStruct>::iterator i = std::find_if(myVector.begin(),
myVector.end(),
[&](const auto& val){ return val.m_id == toFind.m_id; } );
So the [&]
says capture all variables used in the body of the lambda by reference. The (const auto& val)
makes the operator()
of the lambda a template and lets you take in any type. Then in the body we compare what is passed in from find_if
to toFind
.
所以[&]
说通过引用捕获 lambda 主体中使用的所有变量。这(const auto& val)
使得operator()
lambda 成为一个模板,让你接受任何类型。然后在 body 中,我们比较从find_if
to传入的内容toFind
。
回答by Jarod42
You may use the following:
您可以使用以下内容:
MyStruct toFind(1);
std::vector<MyStruct>::iterator i =
std::find_if(myVector.begin(), myVector.end(),
[&](const auto& e) { return e.id == toFind.id; });
回答by Steephen
Do as following:
执行以下操作:
std::find_if(myVector.begin(), myVector.end(),
[&toFind] (const auto &ele) { return ele.m_id == toFind.m_id}; );