Click to See Complete Forum and Search --> : function object comparison of 2 different items for find_if


silvrwood
October 7th, 2004, 03:16 PM
I have a vector<O> vec
O is comprised of:char x, linkedList myList.
myList is a list of chars.
I want to search the vector for O where x is equal to a char which I would pass.

I am trying to use find_if(vec.begin(), vec.end(), ...)

How do I write a function object to pass to find_if that will compare the x member of O to a char passed to it?

wien
October 8th, 2004, 07:22 PM
Quite simply: (Not tested)class my_predicate
{
public:
my_predicate(char p_search_char) : m_search_char(p_search_char)
{}

private:
char m_search_char;

public:
bool operator () (const O& p_current_item)
{
return p_current_item.x == m_search_char;
}
};Usage:std::find_if(vec.begin(), vec.end(), my_predicate('a'));

silvrwood
October 9th, 2004, 03:41 AM
Thank you.