I have a function that searches a vector of iterators and returns the iterator if its names matches a string passed as an argument.
koalaGraph::PVertex lookUpByName(std::string Name, std::vector<koalaGraph::PVertex>& Vertices) {
    for (size_t i = 0; i < Vertices.size(); i++) {
        if(Vertices[i]->info.name == Name) 
            return Vertices[i];
    }
}
My question is how can I implement this as a lambda, to use it in connection with std::find_if? 
I'm trying this:
std::vector<koalaGraph::PVertex> V;
std::string Name;
std::find_if(V.begin(), V.end(), [&Name]() {return Name == V->info.name;})
But it says that V  
an enclosing-function local variable cannot be referenced in a lambda body unless it is in the capture list.
find_if is going to pass the elements of the vector into your lambda.  That means you need
std::find_if(V.begin(), V.end(), [&Name](auto const& V) {return Name == V->info.name;})
so that the V in the lambda body is the element of the vector, not the vector itself.
Ideally you'd give it a different name than V so you keep the vector and local variables separate like
std::find_if(V.begin(), V.end(), [&Name](auto const& element) {return Name == elememt->info.name;})
So now it is clear you are working on a element of the vector, instead of the vector itself.
First, V->info.name is ill formed, inside or outside of the lambda.
The function object sent to the algoritm std::find_if must be a unary function. It must take the current element to check as a parameter.
auto found = std::find_if(
    V.begin(), V.end(), 
    [&Name](koalaGraph::PVertex const& item_to_check) {
        return Name == item_to_check->info.name;
    }
);
The type of found is an iterator to the element that has been found. If none is found, then it returns V.end()
If you use C++14 or better, you can even use generic lambdas:
auto found = std::find_if(
    V.begin(), V.end(), 
    [&Name](auto const& item_to_check) {
        return Name == item_to_check->info.name;
    }
);
                        std::find_if's predicate will receive a reference to each element of the range in turn. You need:
std::find_if(
    V.begin(), V.end(),
    [&Name](koalaGraph::PVertex const &v) { return Name == v->info.name; }
);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With