How to use lambda for std::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
:
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
.
C++ lambda expression in std::find_if?
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};
C++ finding a value using find_if
Return value from find_if
according to reference is
An iterator to the first element in the range for which
pred
does not return false.
pred
is your lambda and it must return bool value.
If you want to use instruction
in your lambda, you have to capture this variable
[instruction] (const pair<string,int>& p) { return p.first == instruction; }
and the call of find_if
when we know it returns iterator looks as follows
auto it = find_if ( begin(machine_opTable), end(machine_opTable), [instruction] (const pair<string,int>& p) { return p.first == instruction; });
if (it != machine_opTable.end())
{
// access to found pair
}
else
; // pair not found
find_if with lambda and capturing variables
You simply have to take a std::pair
(a move) as à parameter inside your lambda:
auto legalMoves = find_if(possible_moves.begin(),
possible_moves.end(),
[](std::pair<int, int> const& a_move)
{
int x, y;
std::tie(x, y) = a_move;
// do whatever you want
});
You could also use auto
:
auto legalMoves = find_if(std::begin(possible_moves), std::end(possible_moves),
[](auto const& a_move) {
int x, y;
std::tie(x, y) = a_move;
// do whatever you want
});
Using find_if with a vector of pointers: How to pass pointer by const reference to lambda?
You want to have const reference to pointer, not reference to const pointer:
[](obj* const& instance) { if(instance->a == 5) return true; return false; }
or with type alias for obj pointer, it is much clearer:
using PtrObj = obj*;
std::find_if(begin(v), end(v), [](const PtrObj& instance) { if(instance->a == 5) return true; return false; });
Related Topics
How to Turn on Multi-Cpu/Core C++ Compiles in the Visual Studio Ide (2008)
C++ Is It Necessary to Delete Dynamically Allocated Objects at the End of the Main Scope
How to Set the Background Image in Qt Stylesheet
What Is Double Evaluation and Why Should It Be Avoided
How to Convert a Tchar Array to Std::String
Is It Good Practice to Make Member Variables Protected
C++ Back End Call the Python Level Defined Callbacks with Swig Wrapper
Static Initialization and Destruction of a Static Library's Globals Not Happening with G++
Gcc Does Not Honor 'Pragma Gcc Diagnostic' to Silence Warnings
Could Not Load Spatialite Extension in Qsqlite ( Qt 5.9)
C++ Regular Expressions with Boost Regex
How to Suppress Specific Warnings in G++
"If" Block Without Curly Braces Makes Subsequent "Else If" Nested
C++ Makefile on Linux with Multiple *.Cpp Files
What Does the |= Operator Mean in C++
Can This MACro Be Converted to a Function