diff --git a/counting_iterator.htm b/counting_iterator.htm new file mode 100644 index 0000000..cd8685e --- /dev/null +++ b/counting_iterator.htm @@ -0,0 +1,186 @@ + + +
+ + + ++Sometimes it would be nice if one could treat an integer like an +iterator, for example, to fill up a vector with the numbers zero +through one hundred using std::copy(). The +only iterator operation missing from builtin integer types is an +operator*(), which we would want to just return the current +value of the integer. The counting iterator adaptor provides this +functionality, though it is a bit more generalized. One can use the +counting iterator adaptor not only with integer types, but with any +type that is incrementable (has operator++()). The following +pseudo-code shows the general idea of how the counting iterator is +implemented. +
+ ++ // inside a hypothetical counting_iterator class... + typedef Incrementable value_type; + value_type counting_iterator::operator*() const { + return this->base; // no dereference! + } ++ +All of the other operators of the counting iterator behave in the same +fashion as the incrementable base type. + +
+namespace boost { + template <class Incrementable> + struct counting_iterator_generator; + + template <class Incrementable> + typename counting_iterator_generator<Incrementable>::type + make_counting_iterator(Incrementable x); +} ++ +
+template <class Incrementable> +class counting_iterator_generator +{ +public: + typedef iterator_adaptor<...> type; +}; ++ +
+#include <boost/config.hpp> +#include <iostream> +#include <boost/counting_iterator.hpp> + +int main(int, char*[]) +{ + // Example of using counting_iterator_generator + std::cout << "counting from 0 to 4:" << std::endl; + boost::counting_iterator_generator<int>::type first(0), last(4); + std::copy(first, last, std::ostream_iterator<int>(std::cout, " ")); + std::cout << std::endl; + + // to be continued... ++ + +
Parameter | Description | +
---|---|
Incrementable | +The type being wrapped by the adaptor. | +
+counting_iterator_generator::type(const Incrementable& i) ++ +
+
+ + +
+template <class Incrementable> +typename counting_iterator_generator<Incrementable>::type +make_counting_iterator(Incrementable base); ++ +This function provides a convenient way to create counting iterators. + +
+ // continuing from previous example... + std::cout << "counting from -5 to 5:" << std::endl; + std::copy(boost::make_counting_iterator(-5), + boost::make_counting_iterator(5), + std::ostream_iterator<int>(std::cout, " ")); + std::cout << std::endl; + + + return 0; +} ++ + +
Revised 10 Feb 2001
+© Copyright Jeremy Siek 2000. Permission to copy, use, +modify, sell and distribute this document is granted provided this copyright +notice appears in all copies. This document is provided "as is" +without express or implied warranty, and with no claim as to its suitability for +any purpose.
+ + + + + + diff --git a/counting_iterator_example.cpp b/counting_iterator_example.cpp new file mode 100644 index 0000000..21d5fb0 --- /dev/null +++ b/counting_iterator_example.cpp @@ -0,0 +1,28 @@ +// (C) Copyright Jeremy Siek 2000. Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + + +#include