I've got a std::map that contains a class and that class has an id. I have an id that I'm trying to find in the set
typedef std::set<LWItem> ItemSet;
ItemSet selectedItems;
LWItemID i = someID;
ItemSet::iterator isi;
isi = std::find_if(selectedItems.begin(), selectedItems.end(), [&a](LWItemID i)->bool { return a->GetID()==i; }
I get an error saying that the lambda capture variable is not found, but I have no idea what I'm supposed to do to get it to capture the container contents as it iterates through. Also, I know that I cant do this with a loop, but I'm trying to learn lambda functions.
You've got your capture and argument reversed. The bit inside the []
is the capture; the bit inside ()
is the argument list. Here you want to capture the local variable i
and take a
as an argument:
[i](LWItem a)->bool { return a->GetID()==i; }
This is effectively a shorthand for creating a functor class with local variable i
:
struct {
LWItemID i;
auto operator()(LWItem a) -> bool { return a->GetID()==i; }
} lambda = {i};
From what i understand you code should look like this :
auto foundItem = std::find_if(selectedItems.begin(), selectedItems.end(),
[&i](LWItem const& item)
{
return item->GetID() == i;
});
This will capture the LWItem that have an ID equal to i, with i being a previosuly declared ID.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.