Constructing One Container from Another : list « list « C++ Tutorial






#include <iomanip>
#include <iostream>
#include <list>
#include <vector> 

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 int data[] = { 1, 1, 2, 3, 5 };
   list<int> original( data,data + sizeof( data ) / sizeof( data[0] ) );

   // make a vector from a list
   vector<int> vector_copy( original.begin(), original.end() );

   // make a list from a list
   list<int> list_copy( original );

   // make a list of floats from a list of ints
   list<float> list_float( original.begin(), original.end() );

   // show results
   print( original);
   print( vector_copy);
   print( list_copy);
   cout << fixed << setprecision( 1 );
   print( list_float );
}








17.1.list
17.1.1.Four constructors of list
17.1.2.Constructing One Container from Another
17.1.3.Use generic list to create a list of chars
17.1.4.Use generic list to create list of strings
17.1.5.Store class objects in a list
17.1.6.Use std::copy to print all elements in a list
17.1.7.Pass list to a function
17.1.8.Uses ostream_iterator and copy algorithm to output list elements
17.1.9.Add elements in a multiset to a list
17.1.10.Add elements in a set to a list