STL find_if functor  
Author Message
hsobel





PostPosted: Mon Sep 12 08:46:43 CDT 2005 Top

Visual Studio C++ >> STL find_if functor

Hi


I am desparately trying to understand STL :(


My current problem is that I have a list of pointers to class event.
Class event has a data member called target (pointer to type
event_handler). I wish to remove the event from the list depending upon

the value of target. Since there is only one event with a prescribed
value, the fastest way to do this would probably be:


myList.erase(find_if(myList.begin(), myList.end(),
someCriterion(target_to_remove));


The problem stems from the need to define the removal criterion. I have

tried the following:


class someCriterion: public std::binary_function<event*,
event_handler*, bool> {
public:
result_type operator()(first_argument_type ev,
second_argument_type
dest) {
if(ev->target =3D=3D dest) return true;
else return false;
} // () overload



};
>From this forum, I learned that the functor must be derived from


binary_function if bind2nd is to be used to permit a binary predicate
to be used in place of a unary predicate.

To remove the event I tried:


std::list<event*>::iterator pos =3D find_if(myList.begin(), myList.end(),

bind2nd(someCriterion(), target_to_remove));
myList.erase(pos);



>From this forum and Josuttis' book I expected the event ptr in the list


to act as the 1st argument of someCriterion and bind2nd to facilitate
placing target_to_remove as the 2nd argument.

The problem with this approach is that it generates an error:
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\functional(321): error C3848: expression having type
'const someCriterion' would lose some const-volatile qualifiers in
order to call 'std::binary_function<_Arg1,_Arg2,_Result>::result_type
sent_to::operator
()(std::binary_function<_Arg1,_Arg2,_Result>::first_argument_type,std::bina=
=ADry_function<_Arg1,_Arg2,_Result>::second_argument_type)'

with
[
_Arg1=3Devent *,
_Arg2=3Devent_handler *,
_Result=3Dbool
]


I have tried making target_to_remove const but to no avail.


Therefore I had a look around this forum, and the following solution
seemed applicable:


class someCriterion: public std::binary_function<event*,
event_handler*, bool> {
public:
someCriterion(first_argument_type ev) : local_event(ev) {};
first_argument_type local_event;
result_type operator()(second_argument_type dest) {
if(local_event->target =3D=3D dest) return true;
else return false;
} // () overload



};


This does not work either :(

In this case, the line:


std::list<event*>::iterator pos =3D find_if(myList.begin(), myList.end(),

bind2nd(someCriterion(), target_to_remove));


generates the following error:


d:\MyDocuments\Visual Studio Projects\802.11 NET\802.11
NET\scheduler.cpp(87): error C2512: 'someCriterion::someCriterion' : no

appropriate default constructor available


I have changed the line to:


std::list<event*>::iterator pos =3D find_if(myList.begin(), myList.end(),

someCriterion(target_to_remove));


This unfortunately generated a different compiler error:


d:\MyDocuments\Visual Studio Projects\802.11 NET\802.11
NET\scheduler.cpp(87): error C2440: 'type cast' : cannot convert from
'event_handler *' to 'someCriterion'


I am sure this must be a very simple problem to solve, however, I have
been unable to find a solution on this forum.


It seems to me that the STL is a bit like dancing with a 3 legged lady.

Sure, she can do all manner of fancy spins and twirls but you have to
watch where you step!=20


Thanks,=20
bob

Visual Studio124  
 
 
Carl





PostPosted: Mon Sep 12 08:46:43 CDT 2005 Top

Visual Studio C++ >> STL find_if functor
> Hi
>
>
> I am desparately trying to understand STL :(

Please don't multipost. This was already answered (twice) in
microsoft.public.vc.stl.

-cd