How to remove element from a vector in C++

Remove elements from vector in C++

In this post, we will see how to remove element from a vector in C++.
There are different methods to remove element in C++ and here we will discuss all of them in detail.

Erase() function to remove a single and specific element from a vector

erase() method can be used to remove a single element by passing the position or with the help of index. Here we have discussed all the methods.

Note: When we remove a single element, the iterator passed must be dereferenceable.

Syntax:
For removing a single element:
vector_name.erase(iterator position); //remove using position

Example: vector1 = {10,20,30}, iterator=2
vector.erase(iterator);
Output: 10, 20

We can mark the beginning and ending of the container with the help of begin() and end() functions respectively.

Code:

Output:

ORIGINAL INTEGER VECTOR ELEMENTS ARE: 1 2 3 4 5 6 7 8
VECTOR AFTER REMOVAL: 1 2 4 5 6 7 8

Erase() function to remove a range of elements from a vector

We can also remove a range of elements by passing first and last position in the erase function.

Syntax:
For removing a range of elements:
vector_name.erase(iterator first, iterator last);

Example: vector1={10, 20, 30, 40, 50, 60, 70, 80}, iterator_first=vector1.begin()+1, iterator_last=vector.end()-2

Output: 10, 70, 80

Code:

Output:

ORIGINAL INTEGER VECTOR ELEMENTS ARE: 1 2 3 4 5 6 7 8
ORIGINAL STRING VECTOR ELEMENTS ARE: java blog c++ python code
VECTORS AFTER REMOVAL:
1 7 8
java python code

Use erase() and remove_if() methods to remove elements that satisfies the condition

We can use the combination of erase() and remove_if() methods to remove elements that satisfy certain criteria and from a specific range as well.

Note: remove_if method does not elements from container. It shifts all elements to front and provide iterator with the end and then we can use erase to delete those elements.

Code:

Output:

Original Vector is: Java Python C++ Java Python
After Removing elements vector is: Python C++ Python

Use erase() and remove() methods to remove elements that satisfies the condition

We can use the combination of erase() and remove() methods to remove elements that satisfy certain criteria and from a specific range as well. We have to declare an extra header to use remove() method algorithm.

Note: This method cannot be used with the containers that return const_iterator.

Code:

Output:

Original Vector is: Java Python C++ Java Python
After Removing elements vector is: Python C++ Python

Remove all the elements in the vector using clear() method

We can empty the vector by using the clear() method. No parameters are passed in this function.

Syntax:
For removing all the elements:
vector_name.clear();

Code:

Output:

Original Vector is: 10 20 30 40 50
After emptying:
//No Output

Conclusion

We discussed different methods to remove a single element or from a range of elements. The time complexity of the erase() method is O(N^2)[because one erase takes linear time] and the clear method is O(N). Any method can be used as per the requirements.

That’s all about how to remove element from vector in C++.
Happy Learning!!

Was this post helpful?

Leave a Reply

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