create an empty vector of int's and assign a list of doubles to it : list vector « list « C++ Tutorial






#include <list>
#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;
   }
}

int main( )
{
   const double data[] = { 3.14, 2.78, 1.51, 7.66, 9.65 };
   list<double> original( data, data + sizeof( data ) / sizeof( data[0] ) );

   vector<double> vector_data( 3, 3.33 ); 
   list<double> list_data( 4, 4.44 );

   // show results
   print( original );
   print( vector_data );
   print( list_data );

   // assign a list to a list
   list_data = original;

   // create an empty vector of int's and assign a list of doubles to it
   vector<int> v;
   v.assign( original.begin(), original.end() );

   // show results
   print( list_data );
   print( v );
}








17.19.list vector
17.19.1.assign a list to a vector
17.19.2.create an empty vector of int's and assign a list of doubles to it
17.19.3.Vector of list of strings
17.19.4.make a vector from a list with vector_copy
17.19.5.Transfer vector to a list and remove all negative numbers with bind2nd and less