Reimundo Heluani Reimundo Heluani - 1 year ago 86
C++ Question

search for an enum in a structure in a vector in c++

I have an

, a structure with this
as a member and a vector of these structures as such:

enum TickerType { tt1, tt2, tt3 };
struct Ticker {
std::string name;
TickerType type;
std::vector<Ticker> vect;

I want to search for an element in
which is of type
. For which I declared

struct find_ticker
const TickerType type;
find_ticker(TickerType type) : type(type) {};
bool operator () ( const Ticker& ticker ) const
return ticker.type == type;

and looked with:

if ( std::find ( vect.begin(), vect.end(), find_ticker ( tt1 ) ) != tickers.end() )

But I get

error: no match for ‘operator==’ (operand types are ‘Dfp::Ticker’ and
‘const {anonymous}::find_ticker’) || { return *__it == _M_value; }

*Dfp is the global namespace of the application and find_ticker is defined in an anoymous namespace in the implementation file where I need it

Answer Source

The std::find function is the wrong function to call if you want to find using a predicate. std::find is for finding based on a value, so you are trying to find a value in the vector that is equal to the find_ticker object.

You should use std::find_if instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download