C++ Array convert to vector

Description

C++ Array convert to vector

#include <iostream>
#include <vector>
using namespace std;
int main()//from   www.  j  a v  a2 s. co 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;
}



PreviousNext

Related