The generator iterator adaptor makes it easier to create custom input iterators from 0-ary functions and function objects. The adaptor takes a Generator and creates a model of Input Iterator. Each increment retrieves an item from the generator and makes it available to be retrieved by dereferencing. The motivation for this iterator is that some concepts can be more naturally expressed as a generator, while most STL algorithms expect an iterator. An example is the Random Number library.
namespace boost { template <class Generator> class generator_iterator_policies; template <class Generator> class generator_iterator_generator; template <class Generator> typename generator_iterator_generator<Generator>::type make_generator_iterator(Generator & gen); }
template <class Generator> class generator_iterator_generator { public: typedef unspecified type; // the resulting generator iterator type }
Parameter | Description |
---|---|
Generator | The generator (0-ary function object) type being wrapped. The return type of the function must be defined as Generator::result_type. The function object must be a model of Generator. |
template <class Generator> typename generator_iterator_generator<Generator>::type make_generator_iterator(Generator & gen);
generator_iterator
transforms a generator into an input iterator.
#include <iostream> #include <boost/generator_iterator.hpp> class my_generator { public: typedef int result_type; my_generator() : state(0) { } int operator()() { return ++state; } private: int state; }; int main() { my_generator gen; boost::generator_iterator_generator<my_generator>::type it = boost::make_generator_iterator(gen); for(int i = 0; i < 10; ++i, ++it) std::cout << *it << std::endl; }