Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ remove_if on a vector of objects

I have a vector (order is important) of objects (lets call them myobj class) where I'm trying to delete multiple objects at a time.

class vectorList {      vector<*myobj> myList;  };  class myobj {      char* myName;     int index;     bool m_bMarkedDelete; } 

I was thinking that the best way to do this would be to mark specific myobj objects for deletion and then call myList.remove_if() on the vector. However, I'm not exactly sure how to use predicates and such for this. Should I create a member variable in the object which allows me to say that I want to delete the myobj and then create a predicate which checks to see if the member variable was set?

How do I implement the predicate as a part of the vectorList class?

like image 206
Jordan Avatar asked Oct 31 '11 18:10

Jordan


People also ask

Can you use delete [] on a vector?

Yes, but it does not go without constraints. There are two ways of deleting a vector. Again they do not go without constraints. One way of deleting a vector is to use the destructor of the vector.

What does Remove_if return C++?

C++ Algorithm remove_if() C++ Algorithm remove_if() function is used to eliminate all the elements that satisfy a predicate from a given range [first, last) without disturbing the order of the remaining elements. This function cannot alter the size of the container. It returns an iterator to the new end of the range.

Why vector is used in priority queue C++?

Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. Priority Queue of Vectors in STL: Priority Queue of Vectors can be very efficient in designing complex data structures.

What is a vector in C __?

In C++, vectors are used to store elements of similar data types. However, unlike arrays, the size of a vector can grow dynamically. That is, we can change the size of the vector during the execution of a program as per our requirements. Vectors are part of the C++ Standard Template Library.


2 Answers

Should I create a member variable in the object which allows me to say that I want to delete the myobj and then create a predicate which checks to see if the member variable was set?

Haven't you already done that? Isn't that what m_bMarkedDelete is for? You would write the predicate like this:

bool IsMarkedToDelete(const myobj & o) {     return o.m_bMarkedDelete; } 

Then:

myList.erase(     std::remove_if(myList.begin(), myList.end(), IsMarkedToDelete),     myList.end()); 

Or, using lambdas:

myList.erase(     std::remove_if(myList.begin(), myList.end(),         [](const myobj & o) { return o.m_bMarkedDelete; }),     myList.end()); 

If your class doesn't actually have that member, and you're asking us if it should, then I would say no. What criteria did you use to decide to mark it for deletion? Use that same criteria in your predicate, for example:

bool IndexGreaterThanTen(const myobj & o) {     return o.index > 10; } 

note -- The functions I've written are of course invalid since all your members are private. So you'll need some way to access them.

like image 156
Benjamin Lindley Avatar answered Sep 28 '22 03:09

Benjamin Lindley


A predicate is basically a conditional comparison. It can be a function or object. Here's an example using new C++ lambdas. This code will go through the vector and remove the values equal to 3.

int arg[6] = {1, 2, 3, 3, 3, 5}; std::vector<int> vec(arg, arg+6); vec.erase(    std::remove_if(       vec.begin(), vec.end(),       [](int i){ return i == 3;}),    vec.end()); 

Edit: For pointers let's say you had a vector or interfaces you could set them to nullptr then remove them in a batch with pretty much the same code. In VS2008 you won't have lambdas so make a comparison predicate function or struct instead.

bool ShouldDelete(IAbstractBase* i) {     return i == nullptr;     // you can put whatever you want here like:     // return i->m_bMarkedDelete; }  std::vector<IAbstractBase*> vec; vec.erase(    std::remove_if(       vec.begin(), vec.end(),       ShouldDelete),    vec.end()); 
like image 27
AJG85 Avatar answered Sep 28 '22 05:09

AJG85