Erase elements in C++ Vector using vector::erase()

C++ STL vector::erase() Example: Here, we are going to learn how to erase elements in C++ Vector.
Submitted by Vivek Kothari, on November 07, 2018

Vectors are sequence container and work like dynamic arrays. Vectors stores elements in contiguous memory locations i.e in a linear sequence. vector::erase() is an inbuilt function in c++ used for removing the specific element from vector or removing the range of elements.

Syntax:

    VectorName.erase (pos);
    VectorName.erase(StartPos, EndPos);

Here, VectorName is the name of vector used at the time of declaration.

Parameters:

  • Pos: It is the position of an element of the vector you want to delete denoted by the iterator.
  • StartPos: It is the iterator specifying a start of the range to be removed.
  • EndPos: It is the iterator pointing to one position ahead of the last element to be removed from the range.

Note: erase(StartPos, EndPos) specifies the range which includes all the elements between StartPosand EndPos, including the element pointed by StartPosbut not include the one pointed by EndPosi. e [StartPos, EndPos).

Example 1: This example shows the basic working of vector::erase() function.

#include<iostream>
#include<vector>
using namespace std;
	
int main ()
{
	std::vector<int> MyVector;
	
	// insert into vector
	for (int i=1; i<=8; i++) MyVector.push_back(i);
	cout << " After elements insertion : "; 
	for (unsigned i=0; i< MyVector.size(); ++i)
		cout << ' ' << MyVector [i];
	
	cout<<endl;
	
	// erase the 3rd element
	MyVector.erase (MyVector.begin()+3);
	cout << " After removing 3rd element: ";
	for (unsigned i=0; i< MyVector.size(); ++i)
		cout << ' ' << MyVector [i]; 
	
	cout<<endl;
		
	// erase the first 3 elements:
	MyVector.erase (MyVector.begin(),MyVector.begin()+3);
	cout << " After removing first three elements: ";
	for (unsigned i=0; i< MyVector.size(); ++i)
		cout << ' ' << MyVector [i]; 	
		
	return 0;
}

Output

 After elements insertion :  1 2 3 4 5 6 7 8
 After removing 3rd element:  1 2 3 5 6 7 8
 After removing first three elements:  5 6 7 8
 

Example 2: Erase all even numbers in a vector.

#include <iostream>
#include <vector>
using namespace std;
int main( )
{
    vector<int> c;
    
    // inserting in vector
    for (int i=1; i<=10; i++) c.push_back(i);
    
    cout<<"Elements in Vector : ";
    for (int i=0; i<=9; i++) {
        cout << c[i] << " ";
    }
    cout <<endl;
 
    // Erase all even numbers
    for (vector<int>::iterator it = c.begin(); it != c.end(); ) {
    	// if number is even remove it
        if (*it % 2 == 0) {
            it = c.erase(it);
        } else {
        	// if number if not even check for next element
            ++it;
        }
    }
 	cout<<"After removing all even numbers : ";
    for (vector<int>::iterator it = c.begin(); it != c.end(); it++ ) {
        cout << *it << " ";
    }
return 0;
}

Output

Elements in Vector : 1 2 3 4 5 6 7 8 9 10
After removing all even numbers : 1 3 5 7 9

Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.