C++ vector erase()

Description

C++ vector erase()

#include <iostream>
#include <vector>
using namespace std;
int main()/*w  ww.j  av  a 2  s  . c  o  m*/
{
   int arr[] = { 100, 110, 120, 130 };  //an array of ints
   vector<int> v(arr, arr+4);       // initialize vector to array
   cout << "\nBefore insertion: ";
   for(int j=0; j<v.size(); j++)    // display all elements
      cout << v[j] << ' ';
   v.insert( v.begin()+2, 115);     // insert 115 at element 2
   cout << "\nAfter insertion:  ";
   for(j=0; j<v.size(); j++)        // display all elements
      cout << v[j] << ' ';
   v.erase( v.begin()+2 );          // erase element 2
   cout << "\nAfter erasure:    ";
   for(j=0; j<v.size(); j++)        // display all elements
      cout << v[j] << ' ';
   cout << endl;
   return 0;
}
#include <iostream> 
#include <array> 
#include <vector> 
#include <ios> 


using namespace std; 

int main(int argc, char *argv [])
{   //from   w ww .  j  a v a 2 s  . com

    using MyVector = vector<int>; 

    MyVector::const_iterator iter = myVector.cbegin() + 1; 
    myVector.insert(iter, 5); 
    myVector.erase(iter); 
        return 0; 
}



PreviousNext

Related