Sign in to follow this  

Please HELP me to solve a list::remove_if problems,With many thanks!

This topic is 4808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HI , I have create the following code, would you please give me some suggestions on how to write the parameter of remove_if(), and the condition I want to use is the element's flag is false? ---------------------------------------------- class PclElem { bool flag; ... } typedef list<PclElem*> PclElemList; PclElemList *pcPclElemList = new PclElemList; // add element to the list and set the flag for each element ... // remove the element that the flag is false pcPclElemList->remove_if(????). ---------------------------------------------- With many thanks! Bst Rgd!

Share this post


Link to post
Share on other sites
What you want is a functor. This is an object of a class implementing the ()-operator like this:


// define a class or a struct which implements flag-checking
// in operator()
class remove_element_functor {
public:
bool operator()( PclElem* _p ) {
// return true if the flag is not set. this causes remove_if
// to delete the element from the list.
// NOTE: this will *not* delete the element itself. You must
// still do this yourself.
return !_p->flag;
}
} remove_element;



You then call remove_if like this:

pcPclElemList->remove_if( remove_element );

Share this post


Link to post
Share on other sites
Thanks for your help, but it dose not work well, and the compiling time error is as below,

-----------------------------
error C2664: 'remove_if' : cannot convert parameter 1 from 'class remove_element_functor' to 'class std::binder2nd<struct std::not_equal_to<class PclElem *> >'
---------------------------

and I reference to the definition of std::list::remove_if,
and the parameter's type is true 'class std::binder2nd<struct std::not_equal_to<class PclElem *> >'.

Then I have try the following code, but it still dose not work well, after call remove_if, all of the element have been removed,
but there must be some element with flag is true.
--------------------------------------------------
class test :public not_equal_to<PclElem*>
{
bool operator()(const PclElem* & rhs, const PclElem* &) const
{
return !rhs->flag;
}

};

PclElem nouse;
nouse.flag = true;

pcPclElemList->remove_if(
binder2nd<not_equal_to<PclElem*> >(test(), &nouse));
--------------------------------------------------
I am very confused, would you please give me some advice again?

Another question about this is, what dose the remove_if function really done, should I need to delete the PclElem object in the list by myself, as the list is
typedef list<PclElem*> PclElemList;
instead of
typedef list<PclElem> PclElemList;

Thank you again!

Share this post


Link to post
Share on other sites
-------------------------------------------------------------
// NOTE: this will *not* delete the element itself. You must
// still do this yourself.
-------------------------------------------------------------
Oh! this is the answer to my second question. Thanks.
Dose that means I must iterate the list and erase the *element itself* at first, and then call remove_if to remove the element of list?

Share this post


Link to post
Share on other sites
Quote:
Original post by Opalm
-------------------------------------------------------------
// NOTE: this will *not* delete the element itself. You must
// still do this yourself.
-------------------------------------------------------------
Oh! this is the answer to my second question. Thanks.
Dose that means I must iterate the list and erase the *element itself* at first, and then call remove_if to remove the element of list?


You can call delete on the pointer inside the functor. Just do something like this:

if( element_is_dead ) {
delete element;
return true;
} else return false;


Sorry, I can't help you with the first problem. I guess that's something specific to the STL implementation you're using. You might try something like this:


class remove_element_functor : public unary_function<PclElem*,bool> {
...
}


This might resolve the type conflict you're getting.

Share this post


Link to post
Share on other sites
Sure, it is a better way to erase the element itself inside the function. Thanks!

but the following definition seems suitable for the std::remove_if function instead of std::list::remove_if.
-----------------------------------------------------
class remove_element_functor : public unary_function<PclElem*,bool> {
...
}
-----------------------------------------------------

Share this post


Link to post
Share on other sites

This topic is 4808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this