2002

Grusel++ of the month

Home
Download Source
/******************************************************************************/
/*                                                                            */
/*                                                              FILE: may.cpp */
/*                                                                            */
/*  CountIterator frees the world of explicit loops !                         */
/*  =================================================                         */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.00   31-MAY-2002   P. Tellenbach   http://www.heimetli.ch/             */
/*                                                                            */
/******************************************************************************/

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

template< class T >
class CountIterator : public std::iterator<std::input_iterator_tag,T>
{
   private:
     T value ;

   public:
     CountIterator( T v ) : value(v)
     {
     }

     bool operator==( const CountIterator &other )
     {
        return value == other.value ;
     }

     bool operator!=( const CountIterator &other )
     {
        return !(*this == other) ;
     }

     const T &operator*()
     {
        return value ;
     }
     
     const T *operator->()
     {
        return &value ;
     }

     CountIterator &operator++()
     {
        value++ ;
        return *this ;
     }

     CountIterator operator++( int )
     {
        CountIterator tmp( *this ) ;
        this->operator++() ;
        return tmp ;
     }
} ;

class Function
{
   private:
     std::string str ;

   public:
     Function() : str( "Hello world !\n" )
     {
     }

     std::ostream &operator()( int index )
     {
        return std::cout.put( str.at(index) ) ;
     }
} ;


int main()
{
   std::for_each( CountIterator<int>(0), CountIterator<int>(14), Function() ) ;

   return 0 ;
}

Update 29. May 2022

This program compiles and runs without any changes.