2002

Grusel++ of the month

Home
Download Source
/******************************************************************************/
/*                                                                            */
/*                                                          FILE: october.cpp */
/*                                                                            */
/*  This function object is missing in the standard                           */
/*  ===============================================                           */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.00   13-OCT-2002   P. Tellenbach   http://www.heimetli.ch/             */
/*                                                                            */
/******************************************************************************/

#include <iostream>
#include <algorithm>
#include <functional>

using namespace std ;

template< class Predicate, class First, class Second >
class unary_evaluator :
   public unary_function<
     typename First::argument_type,
     typename Predicate::result_type
   >
{
   public:
     unary_evaluator( Predicate pred, First fi, Second se ) :
        predicate( pred ), first( fi ), second( se )
   {
   }

   typename Predicate::result_type operator()( typename First::argument_type &arg )
   {
       return predicate( first(arg), second(arg) ) ;
   }

   private:
     Predicate predicate ;
     First     first ;
     Second    second ;
} ;

template< class Predicate, class First, class Second >
unary_evaluator<Predicate,First,Second> eval( Predicate pred, First fi, Second se )
{
   return unary_evaluator<Predicate,First,Second>( pred, fi, se ) ;
}


int main()
{
   string s( "Hexylazlo Aw-Borlady #!\n" ) ;

   remove_copy_if( s.begin(),
                   s.end(),
                   ostream_iterator<char>(cout),
                   eval( logical_and<bool>(),
                         eval( logical_and<char>(),
                               eval( logical_and<char>(),
                                     bind1st(not_equal_to<char>(),'!'),
                                     bind1st(not_equal_to<char>(),'H')
                                   ),
                               eval( logical_and<char>(),
                                     bind1st(not_equal_to<char>(),' '), 
                                     bind1st(not_equal_to<char>(),'\n')
                                   )
                             ),
                         eval( logical_or<char>(),
                               bind1st(greater<char>(),'d'),
                               bind1st(less<char>(),'w') 
                             )
                       )
                 ) ;

   return 0 ;
}

Update 18. October 2022

The include for iterator was missing. No other changes were necessary.

/******************************************************************************/
/*                                                                            */
/*                                                          FILE: october.cpp */
/*                                                                            */
/*  This function object is missing in the standard                           */
/*  ===============================================                           */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.00   13-OCT-2002   P. Tellenbach   http://www.heimetli.ch/             */
/*                                                                            */
/*  Compiled and tested with g++ 11.2.0                                       */
/*                                                                            */
/*  V1.10   18-OCT-2022   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/******************************************************************************/

#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>

using namespace std ;

template< class Predicate, class First, class Second >
class unary_evaluator :
   public unary_function<
     typename First::argument_type,
     typename Predicate::result_type
   >
{
   public:
     unary_evaluator( Predicate pred, First fi, Second se ) :
        predicate( pred ), first( fi ), second( se )
   {
   }

   typename Predicate::result_type operator()( typename First::argument_type &arg )
   {
       return predicate( first(arg), second(arg) ) ;
   }

   private:
     Predicate predicate ;
     First     first ;
     Second    second ;
} ;

template< class Predicate, class First, class Second >
unary_evaluator<Predicate,First,Second> eval( Predicate pred, First fi, Second se )
{
   return unary_evaluator<Predicate,First,Second>( pred, fi, se ) ;
}


int main()
{
   string s( "Hexylazlo Aw-Borlady #!\n" ) ;

   remove_copy_if( s.begin(),
                   s.end(),
                   ostream_iterator<char>(cout),
                   eval( logical_and<bool>(),
                         eval( logical_and<char>(),
                               eval( logical_and<char>(),
                                     bind1st(not_equal_to<char>(),'!'),
                                     bind1st(not_equal_to<char>(),'H')
                                   ),
                               eval( logical_and<char>(),
                                     bind1st(not_equal_to<char>(),' '), 
                                     bind1st(not_equal_to<char>(),'\n')
                                   )
                             ),
                         eval( logical_or<char>(),
                               bind1st(greater<char>(),'d'),
                               bind1st(less<char>(),'w') 
                             )
                       )
                 ) ;

   return 0 ;
}