Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C++ STL

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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates



© https://www.includehelp.com (2015-2018), Some rights reserved.