Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why Vector doesn't provide the remove() member function while list provides?

Tags:

c++

If I want to delete all the elements with a value from vector,I call remove algorithm and then call vector's erase member function to physically delete it. But in the case of list , simple call remove member function and it will delete all elements with that value. I am not sure why vector does't provide the remove MF while list does it.

For Exp: I want to delete value '4' from vector v.

vector<int> v;
vector<int> ::iterator Itr;
for (int i=0; i< 6; i++)
   v.push_back(i*2);
v.push_back(4);
v.push_back(8);
v.push_back(4);
v.erase(remove(v.begin(),v.end(),4), v.end()); 

and for list:

list.remove(4); // will delete all the element which has value 4
like image 917
Alok Avatar asked Jun 22 '11 22:06

Alok


People also ask

Does delete work on vectors?

One way of deleting a vector is to use the destructor of the vector. In this case, all the elements are deleted, but the name of the vector is not deleted. The second way to delete a vector is just to let it go out of scope. Normally, any non-static object declared in a scope dies when it goes out of scope.

How do I remove a specific element from a vector position?

vector::erase() erase() function is used to remove elements from a container from the specified position or range.

How do you remove a value from a vector?

The erase() function can remove an element from the beginning, within, or end of the vector. In order to remove all the elements from the vector, using erase(), the erase() function has to be repeated the number of times there are elements, beginning from the first element.


1 Answers

The question is not why std::vector does not offer the operation, but rather why does std::list offer it. The design of the STL is focused on the separation of the containers and the algorithms by means of iterators, and in all cases where an algorithm can be implemented efficiently in terms of iterators, that is the option.

There are, however, cases where there are specific operations that can be implemented much more efficiently with knowledge of the container. That is the case of removing elements from a container. The cost of using the remove-erase idiom is linear in the size of the container (which cannot be reduced much), but that hides the fact that in the worst case all but one of those operations are copies of the objects (the only element that matches is the first), and those copies can represent quite a big hidden cost.

By implementing the operation as a method in std::list the complexity of the operation will still be linear, but the associated cost for each one of the elements removed is very low, a couple of pointer copies and releasing of a node in memory. At the same time, the implementation as part of the list can offer stronger guarantees: pointers, references and iterators to elements that are not erased do not become invalidated in the operation.

Another example of an algorithm that is implemented in the specific container is std::list::sort, that uses mergesort that is less efficient than std::sort but does not require random-access iterators.

So basically, algorithms are implemented as free functions with iterators unless there is a strong reason to provide a particular implementation in a concrete container.

like image 170
David Rodríguez - dribeas Avatar answered Oct 21 '22 13:10

David Rodríguez - dribeas