// L-12 MCS 360 Mon 20 Sep 2010 : show_function_object.cpp

#include <iostream>
#include <cstdlib>
#include <list>
using namespace std;

class Divisible_By
{
   private:
      int divisor;
   public:
      Divisible_By(int d) : divisor(d){}
      bool operator()(int x)
      {
         return x % divisor == 0;
      }
};

void write ( list<int> L )
{
   for(list<int>::const_iterator item = L.begin();
       item != L.end(); ++item)
      cout << " " << *item;
   cout << endl;
}

void even_numbers ( list<int> L )
{
   list<int>::iterator i = L.begin();

   while(i !=  L.end())
   {
      list<int>::iterator j;

      j = find_if(i,L.end(),Divisible_By(2));

      if(j == L.end()) break;

      cout << " " << *j;

      while(i != j) i++;
      i++;
   }
}

int main()
{
   list<int> L;
   const int n = 20;

   srand(time(0));
   for(int i=0; i<n; i++)
      L.push_back(rand()%100);

   cout << "L : ";
   write(L);
   cout << "the even numbers of L : "
        << endl;
   even_numbers(L); cout << endl;

   return 0;
}

