Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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

Let us see the difference in a tabular form -:

  std::remove vector::erase
1. It is defined in head file <cstdio> It removes single element from the vector.
2.

Its syntax is -:

remove(const char* filename);

Its syntax is -:

iterator erase (const_iterator position);

3. It takes one parameter as filename It returns a random access iterator.
4. It returns 0 if the file is successfully deleted otherwise it returns Non-zero Its time complexity is O(N)


Last Updated : 08 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads