Remove element by value in vector in C++

Remove element by value from vector in C++

In this post, we will see how to remove element by value in vector in C++.

Vectors are a highly recognized concept and are widely used in the world of C++ programming. Vectors are flexible and are utilized in both easy and complex codes. This tutorial focuses on and demonstrates the different ways available to remove elements by value in vector in C++.

What are vectors in C++?

A big part of C++ programming, Vectors can be defined as dynamic arrays that are capable of resizing themselves any time an element is inserted or removed into the given vector. The storage of the vectors is automatically managed by the container. The key to this dynamic ability is the fact that the vector elements are accessed and traversed with the help of iterators easily as they are stored contiguously.

Where can we remove an element from a vector?

An element can be only be inserted at the end of a vector. However, there is no such restriction on removing an element from the vector as it is possible to remove an element from the middle, first, or the last of the vector. We can also remove elements by value in vector in C++.

How to remove element by value in vector in C++?

  • Using the std::find() function.
  • Using the remove() function.
  • Using the remove_copy() function.
  • Using erase() and remove() functions.

The other available methods to remove element by value in vector in C++ namely the standalone erase() method, the std::erase() method, and the remove_if() method can not be utilized in this case as they remove an element from the given vector based on the position of the element rather than the value.

Using the std::find() function to remove element by value in vector in C++.

This is a manual method by which we can remove a single element at once from the given vector if it finds a match with the specified value.

The std::find() function helps in finding an element with the same value as mentioned in the function. Afterward, the found value is then deleted with the help of the generic erase() method.

The following code uses the std::find() function to remove element by value in vector in C++.

The above code provides the following output:

10 40 30 80 20 70

The drawback of using this function is that even though the vector might contain multiple matches of the same value as passed in the function, only one element can be erased from the given vector at once.

Using the remove() function to remove element by value in vector in C++.

We can utilize the remove() function that removes all elements that match a certain specified value. Afterward, the iterator’s position is at the new end of the range of elements of the vector.

The syntax for the remove() function is quite simple and is mentioned below for ease of understanding.

The above function takes in the following parameters:

  • first: The beginning of the specified range of elements. The range includes the first element as well.
  • last: The end of the specified range of elements. The range does not include the last element and it is just a reference point.
  • val: The value that needs to be removed.

The following code uses the remove() function remove element by value in vector in C++.

The above code provides the following output:

java2blog viewers are geniuses really

As we can see from the above code, the remove() function removes the occurrence of the given value hello in the given string vector successfully. However, we noted that the output for int vectors might have some disparity from the expected output.

Using the remove_copy() function to remove element by value in vector in C++.

The remove_copy() function copies all the elements except for the matches values to another vector in order to eliminate some values in vector in C++.

The syntax for the remove_copy() function is quite simple and is mentioned below for ease of understanding.

The above function takes in the following parameters:

  • vector1.begin(): The beginning of the specified range of elements of the original vector.
  • vector1.end: The end of the specified range of elements of the original vector.
  • vector2.begin(): The beginning or the start point of the second vector that is used for the copy process.
  • val: The value that needs to be removed, or in this case, the one which does need to be copied.

The following code uses the remove_copy() function remove element by value in vector in C++.

The above code provides the following output:

50 10 20 20 80 90 40 0 0 0

The element 30 occurs thrice in the original vector and is not copied to the second vector. The 3 spaces of the second vector are taken as 0 by default.

Using erase() and remove() functions to remove element by value in vector in C++.

A combination of the erase() and the remove() functions can be utilized to remove elements by value in vector in C++. To utilize the remove() function, we will have to add an algorithm header to the C++ code.

The following code uses erase() and remove() functions to remove element by value in vector in C++.

The above code provides the following output:

Original Vector is: hello java2blog viewers hello are geniuses really
After Removing elements vector is: java2blog viewers are geniuses really

We should note that this method does not work in the cases where the const_iterator is used.

Conclusion

We discussed four different methods to remove elements by value in vector in C++. The std::find() function only removes one element from the given vector. The remove() function works perfectly for string vectors but might not give the desired output when dealing with int vectors. The other functions work efficiently to remove one or multiple elements matching a given value in vector in C++.

That’s all about how to remove element by value in vector in C++

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *