•  In the solution of Vector-Erase  you will get the knowledge about how to erase elements of vectors in c++  and  how to use the functions of vectors -Erase in c++.
  • Before moving we recommended you  to read this article   vectors in c++


The points are mention below are discussed in this article 

  • problem analysis
  • input format 
  • output format
  • sample input
  • sample output
  • coding
  • coding explanation
  • explain in vs code
  •  feedback

Problem Analysis


first of all you have provided a vector of  'n' integers , then you have given two queries to solve . in the
first query you have to denote a position in vector , The value at that position will be erased .and then update the vector for the second query.


in the next query you have to given again two positions in the vector and in that place the values of vector should be erased.

In this vector-erase solution we are going to use the some function that will help us to erase the element
in the vector (erase function).



Vector-Erase in c++

Vector-Erase in c++



Input format

  • In the first line of the input contains an integer  'n' the next line contains  'N' space separated by integers .
  • The third line contains a single integer x  denoting the position of an element that should be removed from the vector .
  • The fourth line contains two integers  a  and   b  denoting the range that should be erased from the vector inclusive of a and exclusive of b.


Output format


  • Print the size of the vector in the first line and print the elements of the vector after the two erase operations in the second line separated by a space.


Sample input


6
1 4 6 2 8 9
2
2 4

Sample output


3
1 8 9

Coding

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v ;
    int x,i,j; 
    int n; 
    cin>>n;
    for(i =0 ; i<n ; i++)
    {
        cin>>x; 
        v.push_back(x);
    }
    int q1, q2, q3;
    cin>>q1;
    cin>>q2>>q3;
    v.erase (v.begin()+(q1-1));
    v.erase(v.begin()+q2-1 , v.begin()+q3-1);
    cout << v.size() << endl;
    for(j =0 ; j< v.size() ; j++)
    {
        cout << v.at(j) << " " ;  
    }
    return 0;
}


Coding explanation


  • First of all declared the required header files for the vector-erase solution.
  • Then we declare the vector of int type of name 'v' after that we declare some variables for taking the size of vectors and for positions to erase elements.
  • Here 'n' variable is for taking the size of the vector and 'x' variable is for taking input the vectors by the help of push_back function.
  • Then we declare three variable of name q1,q2,q3 for erasing the queries ,in first line we take the 1st query to erase from the vector and then we take two more positions for erasing the elements.
  • Now we use the erase function for deleting values for the 1st query and second query then we print the size of vector now present in vector and then we print in a next line the elements are present in vector.



Vs code example for reference not to use in hacker rank


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v ;
    int x,i,j; 
    int n,count=1; 
    cout<<"Enter the size of vector:"<<endl;
    cin>>n;
    for(i =0 ; i<n ; i++)
    {
        cout<<"Enter the elements no. :"<<count++<<endl;
        cin>>x; 
        v.push_back(x);
    }
    int q1, q2, q3;
    cout<<"Enter the first position that you want to erase:"<<endl;
    cin>>q1;
    cout<<"Enter the next two positions for erasing the data:"<<endl;
    cin>>q2>>q3;
    v.erase (v.begin()+(q1-1));
    v.erase(v.begin()+q2-1 , v.begin()+q3-1);
    cout<<"The number element present now in vector is:"<<endl;
    cout << v.size() << endl;
    cout<<"The vectors are:"<<endl;
    for(j =0 ; j< v.size() ; j++)
    {
        cout << v.at(j) << " " ;  
    }
    return 0;
}


Output 

Enter the size of vector:
6
Enter the elements no. :1
1
Enter the elements no. :2
4
Enter the elements no. :3
6
Enter the elements no. :4
2
Enter the elements no. :5
8
Enter the elements no. :6
9
Enter the first position that you want to erase:
2
Enter the next two positions for erasing the data:
2 4
The number element present now in vector is:
3
The vectors are:
1 8 9



Also check Out





  Q-2)  pointers in c




  

   Q-3) Vector Sort


Also check Out Projects for Resume

    click here

Projects on C++


Projects on C


Problems On C


Problems On C++



Feedback



  • That is for Vector-erase  in C++ Hacker Rank solution   hope you enjoy  the article and understand this explanation clearly , if still have you any doubts please comment below and if any suggestion for us give us through the comment and please share the article with your friends if you like this.




THANKYOU!!