Getting a C-Style Array from a Vector : object vector « Vector « C++






Getting a C-Style Array from a Vector

  
#include <numeric>
#include <vector>
#include <iostream>

using namespace std;

template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}

void doubler( int a[], int length ){
   for( int i = 0; i < length; ++i )
      a[i] *= 2;
} 

int main( ){
   vector<int> data( 5, 1 );
   partial_sum( data.begin(), data.end(), data.begin() );
   print( data );

   if( !data.empty() ) {
      doubler( &data[0], static_cast<int>( data.size() ) );
      print( data );
   }
}
  
    
  








Related examples in the same category

1.Call member function for each element in vector
2.Sort vector of user-defined values
3.Store user-defined objects in a vector.