Transfer vector to a list and remove all negative numbers with bind2nd and less : list vector « list « C++ Tutorial






#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#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 float a[] = { 1, -2.3, 4.5, -6.7, 8.9, 10.2};

   // create and initialize vector with above data
   vector<float> data( a,a + sizeof( a ) / sizeof( a[0] ) );

   cout << "DATA VECTOR HAS " << data.size() << " ELEMENTS\n";
   print( data  );

   // transfer to a list and remove all negative numbers
   list<float> l( data.begin(), data.end() );
   cout << "\nSize of list before calling remove_if: "<< l.size() << endl;
   l.remove_if( bind2nd( less<float>(), 0 ) );
   cout << "Size of list after calling remove_if: " << l.size() << endl;
   print( l );
}








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