Remove items with a specific value from std :: list

I need to remove items with a specific value from std :: list. Using list<int> I used the remove () method.

Now I have a list<CMyClass> , so I thought I should use remove_if (), but the predicate takes only one parameter - the item to be checked.

How to write a function foo(const CMyClass &Bad) that removes all elements equal to Bad from the list?

thanks

PS

 struct CMyClass { void *Ptr; int Var; } bool is_equal(const CMyClass &A, const CMyClass &B) { if (A.Ptr == B.Prt and A.Var == B.Var) return true; else return false; } 
+4
source share
7 answers

Your class must implement operator == in its ClassName

 bool operator == ( const Class& rhs ); 

and then you can use

 list.remove( Bad ) 

If it is reasonable for your class to have the == operator (not just for deletion), then the list :: remove list is right for you. If the == operator is only for the :: remove list, then it is better to use remove_if.

The following example shows the list :: remove and list :: remove_if.

 struct Class { int a_; int b_; Class( int a, int b ): a_( a ), b_( b ) {} bool operator == (const Class &rhs) { return (rhs.a_ == a_ && rhs.b_ == b_); } void print() { std::cout << a_ << " " << b_ << std::endl; } }; bool isEqual( Class lhs, Class rhs ) { return (rhs.a_ == lhs.a_ && rhs.b_ == lhs.b_); } struct IsEqual { IsEqual( const Class& value ): value_( value ) {} bool operator() (const Class &rhs) { return (rhs.a_ == value_.a_ && rhs.b_ == value_.b_); } Class value_; }; int main() { std::list<Class> l; l.push_back( Class( 1, 3 ) ); l.push_back( Class( 2, 5 ) ); l.push_back( Class( 3, 5 ) ); l.push_back( Class( 3, 8 ) ); Class bad( 2, 5 ); std::cout << "operator == " << std::endl; l.remove( bad ); std::for_each( l.begin(), l.end(), std::mem_fun_ref( &Class::print ) ); std::cout << "binary function predicat" << std::endl; l.push_back( Class( 2, 5 ) ); l.remove_if( std::bind2nd( std::ptr_fun(isEqual), bad ) ); std::for_each( l.begin(), l.end(), std::mem_fun_ref( &Class::print ) ); std::cout << "functor predicat" << std::endl; l.push_back( Class( 2, 5 ) ); l.remove_if( IsEqual( bad ) ); std::for_each( l.begin(), l.end(), std::mem_fun_ref( &Class::print ) ); return 0; } 
+15
source

To remove all elements according to a predicate:

 struct Pred { bool operator()(int i) const { return i == 42; } }; std::list<int> l = ...; l.remove_if(Pred()); 

or delete all items with a value of 42:

 std::list<int> l = ...; l.remove(42); 

or for the CMyClass list:

 struct Pred { bool operator()(const CMyClass& item) const { return item.GetSomething() == 42 && item.GetSomethingElse() == 314159; } }; std::list<CMyClass> l = ...; l.remove_if(Pred()); 
+6
source

Create a class that takes the value bad and stores it in a member variable. Then do

 bool operator()(CMyClass const &currVal) { return (currVal is equal to this->bad); } 

Pass an instance of this remove_if object

+3
source

You can simply iterate over elements, compare and erase if the condition matches, for example:

 for (std::list<string>::iterator i = mylist.begin(), e = mylist.end(); i != e; ) { if (*i == "foobar") i = mylist.erase(i); else ++i; } 
+2
source

Execute the == operator (const CMyClass &).

eg:.

 #include <list> #include <iostream> #include <iterator> using namespace std; class CMyClass { public: CMyClass(const int d) : data(d) {} bool operator==(const CMyClass &rhs) { return data == rhs.data; } friend ostream& operator<<(ostream &ost, const CMyClass &rhs) { return ost << rhs.data; } private: int data; }; int main(int, char **) { list<CMyClass> li; CMyClass a(1); CMyClass b(8); li.push_back(a); li.push_back(b); copy(li.begin(), li.end(), ostream_iterator<CMyClass>(cout,"\n")); li.remove(a); copy(li.begin(), li.end(), ostream_iterator<CMyClass>(cout,"\n")); return 0; } 

Result:

1 8 8

0
source
 using namespace std; class Foo { public: Foo(int newVal){value = newVal;} int value; }; class checkEqual : public binary_function<Foo, Foo, bool> { public: bool operator()(const Foo &inputOne, const Foo &inputTwo) const { return inputOne.value == inputTwo.value;} // implement your comparison here }; int main(int count, char** args) { list<Foo> myList; for(int x = 0; x < 10; ++x) myList.push_back(Foo(x)); Foo bad(5); myList.remove_if(bind2nd(checkEqual(), bad)); return 0; } 

Or, if you want to overload ==, you can do:

 class Foo { public: Foo(int newVal){value = newVal;} int value; bool operator==(const Foo &other) const { return this->value == other.value;} // your comparison here }; 

And then:

 myList.remove(bad); 
0
source
-1
source

All Articles