using lambda expression and find_if on a collection

c++, lambda, stl

Solution

Missing a bit of context, but I note:

- You return `**it` so you possibly want to compare `*v==itemt`

- You pass `const R&v` where I suspect you meant `const T&v` into the lambda

- You used a const_iterator, but returned a non-const reference. That was a mismatch

- I made some params const& for efficiency (and to support non-copyable/non-movable types)

Here is working code, stripped of the missing class references:

#include <vector>
#include <algorithm>
#include <iostream>

template<typename T, typename R=std::vector<T> >
T& Find(R& Container, T const& item ) 
{   
    typename R::iterator it = std::find_if(Container.begin(), Container.end(),  [&item](const T&v) { return item == v; });
    if (it != Container.end())
        return *it;
    else
        throw "TODO implement";
}

int main(int argc, const char *argv[])
{
    std::vector<double> v { 0, 1, 2, 3 };
    Find(v, 2.0); // not '2', but '2.0' !
    return 0;
}

Problem

I have a container object : ``` R Container; ``` R is of type `list<T*>` or `vector<T*>` I am trying to write the following function: ``` template<typename T, typename R> T& tContainer_t<T, R>::Find( T const item ) const { typename R::const_iterator it = std::find_if(Container.begin(), Container.end(), [item](const R&v) { return item == v; }); if (it != Container.end()) return (**it); else throw Exception("Item not found in container"); } ``` When trying the method (v is an object of my class) ``` double f = 1.1; v.Find(f); ``` I get `binary '==' : no operator found which takes a left-hand operand of type 'const double' (or there is no acceptable conversion)` I am confused with the lambda expression syntax and what i should write there and couldn't find any friendly explanation. What is wrong ? 10x.

Original source