Question

In this function, I search for the pair.first in the vector cache. The vector is :

vector<pair<double,unsigned int> > cache;

The custom function I have used for the find function is :

struct comp
{
    comp(double const& s) : _s(s) { }

    bool operator () (pair<double, unsigned int> const& p)
    {
        return (p.first == _s);
    }

    double _s;
};

I call the find function as :

it = find(cache.begin(),cache.end(),comp(value));

On compilation, I get a lot of errors. The first few lines are :

In file included from /usr/include/c++/4.6/algorithm:63:0, from my_class.hpp:5, from main.cpp:5: /usr/include/c++/4.6/bits/stl_algo.h: In function ‘RandomAccessIterator std::_find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator*, std::vector > >, _Tp = MyCode::MyClass::comp]’: /usr/include/c++/4.6/bits/stl_algo.h:4326:45: instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator*, std::vector > >, _Tp = MyCode::MyClass::comp]’ my_class.hpp:76:53: instantiated from here /usr/include/c++/4.6/bits/stl_algo.h:162:4: error: no match for ‘operator==’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = std::pair*, _Container = std::vector >, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = std::pair& == __val’

How can I solve this error ?

Was it helpful?

Solution

You're passing a predicate, not a value, so you need find_if(), not find().

OTHER TIPS

Your call should be using std::find_if, instead of std::find.

 it = find_if(cache.begin(),cache.end(),comp(value));
 if ( it != cache.end() )
 {


 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top