Difference between std::remove and vector::erase for vectors

20 Jun, 2022
std::remove : It doesn’t actually delete elements from the container but only shunts non-deleted elements forwards on top of deleted elements. vector::erase : Removes from the vector either a single element (position) or a range of elements ([first, last)).
std::remove vs vector::erase
By using erase all elements in a std::vector will be shifted by 1 causing a large amount of copies; std::remove does just a ‘logical’ delete and leaves the vector unchanged by moving things around.
If you need to remove multiple elements from the vector, the std::remove will copy each, not removed element only once to its final location, while the vector::erase approach would move all of the elements from the position to the end multiple times.
For Example, Consider removing all elements < 5 in following vector.
std::vector v { 1, 2, 3, 4, 5 };
// remove all elements < 5
Using erase, If you went over the vector removing elements one by one, you would remove the 1, causing copies of the remainder elements that get shifted (4). Then you would remove 2 and shift all remaining elements by one (3)... if you see the pattern this is a O(N^2) algorithm. In the case of std::remove the algorithm maintains a head, and iterates over the container. For the first 4 elements the head will be moved and the element tested, but no element is copied. Only for the fifth element the object would be copied from the last to the first position, and the algorithm will complete with a single copy and returning an iterator to the second position. This is a O(N) algorithm. The later std::vector::erase with the range will cause destruction of all the remainder elements and resizing the container.
Therefore, erase() is something you can do to an element in a container, remove() is something you can do to a range as it re-arranges that range but doesn’t erase anything from the range..
CPP
// CPP program to illustrate// difference b/w std::remove// and std::vector::erase algorithm#include <bits/stdc++.h> int main(){    std::vector<int> vec{ 10, 20, 30, 30, 20, 10, 10, 20 };    std::vector<int> ve{ 10, 20, 30, 30, 20, 10, 10, 20 };     // Print original vector    std::cout << "Original vector :";    for (int i = 0; i < vec.size(); i++)        std::cout << " " << vec[i];    std::cout << "\n";     // Iterator that store the position of last element    std::vector<int>::iterator pend;     // std :: remove function call    pend = std::remove(vec.begin(), vec.end(), 20);     // Print the vector after std :: remove    std::cout << "Range contains:";    for (std::vector<int>::iterator p = vec.begin(); p != pend; ++p)        std::cout << ' ' << *p; std::cout << '\n';             // Print original vector            std::cout << "Original Vector :";    for (int i = 0; i < ve.size(); i++)        std::cout << " " << ve[i];    std::cout << "\n";     // std :: vector :: erase function call    // erase the first 3 elements of vector    ve.erase(ve.begin(), ve.begin() + 3);     // Print the vector    std::cout << "Vector contains :";    for (int i = 0; i < ve.size(); i++)        std::cout << " " << ve[i];    std::cout << "\n";     return 0;}
Output:
Original vector : 10 20 30 30 20 10 10 20
Range contains: 10 30 30 10 10

Original Vector : 10 20 30 30 20 10 10 20
Vector contains : 30 20 10 10 20
Reference : Stack Overflow This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Let us see the difference in a tabular form -:
Its syntax is -:
remove(const char* filename);
Its syntax is -:
iterator erase (const_iterator position);
surinderdawra388
mayank007rawa
rkbhola5
cpp-algorithm-library
STL
C++
Difference Between
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.