Use indexer to update element in the vector : vector indexer « Vector « C++






Use indexer to update element in the vector

   
 

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

#include <vector>    
#include <algorithm> 
#include <iterator>  
#include <stdexcept> 

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 );

   integers[ 0 ] = 7; // set first element to 7

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

   return 0;
}

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

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








Related examples in the same category

1.Use toupper function to convert all elements in a char vector to upper case
2.make the index of the maximum
3.Fill in vector with array by using pointer and offset