Table of contents
1.
Introduction
2.
C++ Vectors
3.
C++ vector erase function
4.
Syntax of Vector erase() in C++
5.
Parameters of Vector erase() C++
6.
Return Value of C++ Vector erase()
6.1.
Example of c++ vector erase function
6.2.
Implementation of vector erase function in C++
6.2.1.
Example 1
6.3.
C++
6.3.1.
Example 2
6.4.
C++
7.
clear() vs erase(), when to use what?
8.
Frequently Asked Questions
8.1.
Are vectors better than arrays in C++?
8.2.
What does STL mean in C++?
8.3.
Does vector erase reduce the size of the array?
9.
Conclusion
Last Updated: Apr 1, 2024
Easy

How to work with erase() in vector in C++?

Author Ashish Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A vector is a dynamic array that is built into a standard C++ library. This vector class can grow in size dynamically. The elements are arranged in a continuous manner so that iterators can traverse them.

In this article, we will see an introduction to vector array, c++ vector erases function, clear function, syntax, example, and implementation in C++.

vector in c++

C++ Vectors

Vectors are used in C++ to store elements of related data types. We can adjust the size of the vector during the execution of a program to solve our problems. The C++ Standard Template Library includes vectors. We must include the vector header file in our program to use vectors.

To include the vector library in your program, use the below command:

#include<vector>

C++ vector erase function

  • The vector erase() function is implemented in the vector class and is used to remove elements one at a time. 
  • The vector erase() function is used to remove the elements from a specified position. 
     

Vector erase() function is of two types:

🔺vector.erase(position) -It specifies the element's position that needs to be removed from the vector.

🔺vector.erase(start position, end position) -It specifies the range of the element which needs to be removed from the vector.

erase function

Syntax of Vector erase() in C++

iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);
  • The erase() function in C++ vector is used to remove elements from a vector.
  • It can remove a single element specified by its position or a range of elements specified by iterators first and last.
  • The first syntax removes the element at the specified position.
  • The second syntax removes the elements in the range [first, last) where first is inclusive and last is exclusive.
  • It returns an iterator pointing to the element that follows the last element removed.

Parameters of Vector erase() C++

The erase() function in C++ vector takes one or two parameters:

1. position:

  • Specifies the position of the element to be removed. It is an iterator pointing to the element to be erased.

2. first, last:

  • Specifies a range of elements to be removed.
  • first is an iterator pointing to the first element of the range (inclusive).
  • last is an iterator pointing to the element just beyond the last element of the range (exclusive).

Return Value of C++ Vector erase()

  • The erase() function returns an iterator pointing to the element that follows the last element removed.
  • If a single element is removed, the returned iterator points to the element succeeding the removed element.
  • If a range of elements is removed, the returned iterator points to the element succeeding the last element removed in that range.
  • If no elements are removed, the function returns the iterator pointing to the element specified by the position parameter.

Example of c++ vector erase function

Let’s discuss the example that helps us understand the c++ vector erase() function easily. Suppose you have this vector array 

Now let’s assume that our iterator is pointing to element 3. Your vector.erase() function will remove the element which is pointing to a location.

The array after the deletion of the element will look like

Implementation of vector erase function in C++

Example 1

  • C++

C++

#include <iostream>
#include <vector>
using namespace std;
int main()
{
// initialising the vector with parameters
vector<int> vect{15, 20, 1};

// calling the vector erase function to remove the element from
vect.erase(vect.begin() + 1);

//the position of beginning+1.                     
for (int j = 0; j < vect.size(); j++)
cout<< vect[j] << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output

output


You can also practice with the help of Online C++ Compiler 

Example 2

  • C++

C++

#include <iostream>
#include <vector>
using namespace std;

int main()
{
// initialising the vector with parameters
vector<string> fruits{"apple", "mango", "strawbery", "kiwi", "banana"};
cout << "fruit names are :";
for (int j = 0; j < fruits.size(); j++)
cout << fruits[j] << " ";
cout << endl;

// calling the vector erase function to remove the element
fruits.erase(fruits.begin() + 1, fruits.begin() + 3);

// from the range of beginning+1 and beginning+3.
cout << "After removing the apple and strawbery fruits," << '\n';

for (int j = 0; j < fruits.size(); j++)
cout << fruits[j] << " ";
return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output

output

clear() vs erase(), when to use what?

clear():

  • Use clear() when you want to remove all elements from the vector and make it empty.
  • This function does not take any parameters.
  • It is typically used when you no longer need the contents of the vector and want to free up memory.

erase():

  • Use erase() when you want to remove specific elements from the vector based on their position or a range.
  • This function can remove a single element at a specified position or a range of elements specified by iterators.
  • It is used when you need to selectively remove elements from the vector while retaining others.

Frequently Asked Questions

Are vectors better than arrays in C++?

Vectors are better suited for frequent insertion and deletion, whereas arrays are significantly better suited for frequent element access. Arrays are a memory-efficient data structure, whereas Vectors take more memory in exchange for managing storage.

What does STL mean in C++?

C++ STL (standard template library) is a C++ standard library containing templates representing containers, iterators, algorithms, and function objects.

Does vector erase reduce the size of the array?

Yes, vector erase reduces the size when you erase the elements. Vector returns the number of elements present in the vector. 

Conclusion

In the above article, we have extensively discussed the introduction to vector array, c++ vector erases function, clear function, syntax, example, and implementation in c++.

After reading about c++ vector erase, are you not feeling excited to read/explore more articles on c++? Don't worry; Coding Ninjas has covered other articles also. If you want to check out articles related to c++, then you can refer to these links,

NaN in C++New and delete operator in c++Scope resolution operator vs this pointer, Ternary Operator in C .

Refer to our Guided Path on Coding Ninjas Studio to increase your knowledge in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! 


If you want to test your coding skills, you can check out the mock test series and can participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning! 

Live masterclass