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!!

Related Posts

  • Merge two vectors in C++
    11 June

    Merge two Vectors in C++

    Table of ContentsUsing merge() to merge two vectors in C++Using set_union() to merge two vectors in C++Using insert() to combine two vectors in C++Using move() & back_inserter to merge two vectors in C++Using copy() and back_inserter to concatenate two vectors in C++Using make_move_iterator() and insert() to merge two vectors in C++Conclusion vectors are like dynamic […]

  • Vector of Structs in C++
    18 May

    Vector of structs in C++

    Table of ContentsUsing Range ConstructorUsing Initializer listUsing custom constructorBonus ProgramConclusion In this article, we will see how to initialize and use vector of structs in C++. There are different ways to do it. This article will explain these methods clearly. Using Range Constructor We can utilize the range constructor for the initialization of vector of […]

  • vector of vectors C++
    13 April

    How to create vector of vectors in C++

    Learn about how to create vector of vectors in C++.

  • 13 April

    Vector in C++

    Learn about vector in C++. It also cover various functions of vector in C++.

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.