Insert elements from array : vector insert « Vector « C++






Insert elements from array

   
 

#include <iostream>
using std::cout;
using std::endl;

#include <vector>    // vector class-template definition
#include <algorithm> // copy algorithm
#include <iterator>  // ostream_iterator iterator
#include <stdexcept> // out_of_range exception

int main()
{
   int array[ 6 ] = { 1, 2, 3, 4, 5, 6 };
   std::vector< int > integers( array, array + 6 );
   std::ostream_iterator< int > output( cout, " " );

   integers.push_back( 2 );
   integers.push_back( 3 );
   integers.push_back( 4 );

   cout << "Vector integers contains: ";
   std::copy( integers.begin(), integers.end(), output );

   // insert elements from array
   integers.insert( integers.begin(), array, array + 6 );
   cout << "\n\nContents of vector integers before clear: ";
   std::copy( integers.begin(), integers.end(), output );


   return 0;
}

/* 
Vector integers contains: 1 2 3 4 5 6 2 3 4

Contents of vector integers before clear: 1 2 3 4 5 6 1 2 3 4 5 6 2 3 4 
 */        
    
    
  








Related examples in the same category

1.Insert element by index
2.Insert element at specific position
3.Insert 10 duplicate value to vector
4.Insert one vector to another vector
5.Combine insert and end to add elements to the end of vector
6.Insert characters into vector. An iterator to the inserted object is returned
7.Create and initialize vector with float number array
8.Appending values to Vector Containers after sorting
9.Inserts an element into a vector at given position