简体   繁体   English

如何将lambda用于std :: find_if

[英]How to use lambda for std::find_if

I am trying to use std::find_if to find an object that matches some criteria. 我试图使用std :: find_if来查找符合某些条件的对象。 Consider the following: 考虑以下:

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. 我见过的所有示例都有一个lambda,它使用硬编码值来检查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. 我想要的是只有当toFind的id与向量中某个项的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()进行另一个案例)

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定义了适当的==重载, MyStruct可以使用find

std::find(myVector.begin(), myVector.end(), toFind);  // requires ==

The find_if version is usually best when you have some kind of heterogeneous lookup, for example if you were just given an int , not a value of MyStruct . 当你有某种异构查找时, find_if版本通常是最好的,例如,如果你只是给了一个int ,而不是MyStruct的值。

This is where the lambda capture comes into play. 这是lambda捕获发挥作用的地方。 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. 除了说明将哪种类型的参数传递给lambda之外,您还可以说明使用哪些现有变量来构造lambda。 So in this case you would have something like 所以在这种情况下你会有类似的东西

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. 所以[&]表示通过引用捕获lambda体中使用的所有变量。 The (const auto& val) makes the operator() of the lambda a template and lets you take in any type. (const auto& val)使lambda的operator()成为模板,并允许您接受任何类型。 Then in the body we compare what is passed in from find_if to toFind . 然后在正文中我们比较从find_if传递到toFind

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

Do as following: 请执行以下操作:

std::find_if(myVector.begin(), myVector.end(), 
          [&toFind] (const auto &ele) { return ele.m_id == toFind.m_id}; );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM