I have a class which has a map. I need to find iterator in map by searching for a particualar value, instad of the key. Using a member function predicate IsValueFound, am trying this.
class A
{
public:
void findVal();
private:
int state;
map<int, int> exmap;
bool IsValueFound(pair<int key, int val> itr)
{
return state == itr.second;
}
};
void A::findVal
{
itr = find_if(exmap.begin, exmap.end, mem_fun1_ref(&A::IsValueFound));
}
Iam getting compilation errors. Iam not sure whats the syntax for these function adaptors. Please help.
EDIT: Sorry. Please neglect the compilation errors arising other than from finf_if stmt. I need to get find_if stmt corrected first. Also the code doesn’t have boost 🙁
Edit: there’s obviously an error in my answer,
mem_fun1_ref(&A::IsValueFound)doesn’t work as a predicate forstd::find_if. I’m working on correcting that.you forgot parenthesis with
exmap.beginandexmap.end.I think if you had read the compilation error report, it would have told seomthing about it.
I would write that this way:
but I havent’t tried
mem_fun1_ref(&A::IsValueFound)to compile. and I’m not used to usingmem_fun1_ref, I always redefine my own functor with theiroperator().