Coders Packet

Remove all elements by value from a vector

By Ashutosh Sunil Rane

In this program, we will see how we can remove all the elements of a particular value in a specified range from the vector.

This tutorial will see how we can remove all the elements of a particular value from the vector.

Vectors are template class containers in the Standard Template Library (STL) of C++. Vectors have the ability to resize themselves accordingly when elements are inserted or deleted, and can automatically manage storage. Vectors are dynamic arrays and hence are efficient If you perform many inserts or delete operations.

The syntax for vector is :

 vector vector_name; 

 

A vector can contain duplicate elements unlike sets and are stored in the way they are inserted.

vector v={1,1,1,2,3,4,1};  //here vector v has four elements with value 1

 

In this tutorial, we want to see how we can remove all the elements of a particular value from the vector.

For example, we want to remove all the elements of value 1 from the vector v so that the resulting vector will not have any element with value 1.

Input :
1,1,1,2,3,4,1
1
Output : 2,3,4

 

Vector Erase Function

The Vector class has various functions to perform different operations. One such function is vector erase();

The erase() function can remove one value by position or multiple values in a range from the vector without changing the order of other elements.

The erase function can take the position of the element to be removed or the range.

Syntax:

vector.erase(position_iterator)
vector.erase(begin_iterator,end_iterator)

 

std::remove in C++

the std::remove function removes all elements of value equal to a specified number between the specified range without changing the order of other elements. The function also cannot change the size of the array or container.

The remove function is defined in the algorithm library in C++. So we need to include the algorithm library to use the std::remove function.

The remove function takes three parameters.

Syntax : 

std:remove(begin_iterator,end_iterator,value)
//begin_iterator: points to the starting position of the range //end_iterator: points to the ending position of the range //value: element value to remove

 

Program

Now combining these two functions we can remove all elements of a particular value from a vector.

Example:

#include 
#include 
#include 

int main()
{
    std::vector vec;
    vec.push_back(1);
    vec.push_back(1);
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(1); //Now the Vector becomes 1,1,1,2,3,4,1

    int remove_element_value = 1; //element to be reoved;

    //printing all elements before removing operation
    std::cout << "Elements in vec before removing : " << std ::endl;
    for (int num : vec)
        std::cout << num << " ";

    //removing all elements with value remove_elment_value
    vec.erase(std::remove(vec.begin(), vec.end(), remove_element_value), vec.end());

    //printing all elements after removing operation
    std::cout <<std::endl<< "Elements in vec after removing : " << std::endl;
    for (int num : vec)
        std::cout << num << " ";

    return 0;
}

Output

Elements in vec before removing :
1 1 1 2 3 4 1
Elements in vec after removing :
2 3 4

 

Program to delete elements by value in a range in vector 

We can also delete the duplicate elements in a specified range.

for example, in the above code, we can only remove value 1 between index 1 to 6.

 

Program

#include 
#include 
#include 

int main()
{
    std::vector vec;
    vec.push_back(1);
    vec.push_back(1);
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(1); //Now the Vector becomes 1,1,1,2,3,4,1

    int remove_element_value = 1; //element to be reoved;
    int start = 1;                //starting postion in range
    int end = 6;                  //ending position in range
    int n = vec.size();           //size of vector

    //printing all elements before removing operation
    std::cout << "Elements in vec before removing : " << std ::endl;
    for (int num : vec)
        std::cout << num << " ";

    //removing all elements with value remove_elmennt between range (start,end)
    vec.erase(std::remove(vec.begin() + start, vec.begin() + end, remove_element_value), vec.begin() + end);

    //printing all elements after removing operation
    std::cout << std::endl<< "Elements in vec after removing : " << std::endl;
    for (int num : vec)
        std::cout << num << " ";

    return 0;
}

Output

Elements in vec before removing :
1 1 1 2 3 4 1
Elements in vec after removing :
1 2 3 4 1