diff --git a/transform_iterator.htm b/transform_iterator.htm new file mode 100644 index 0000000..6100817 --- /dev/null +++ b/transform_iterator.htm @@ -0,0 +1,189 @@ + + +
+ + + ++The transform iterator adaptor augments an iterator by applying some +function object to the result of dereferencing the iterator. Another +words, the operator* of the transform iterator first +dereferences the base iterator, passes the result of this to the +function object, and then returns the result. The following +pseudo-code shows the basic idea: + +
+ value_type transform_iterator::operator*() const { + return f(*this->base_iterator); + } ++ +All of the other operators of the transform iterator behave in the +same fashion as the base iterator. + + +
+namespace boost { + template <class AdaptableUnaryFunction, class BaseIterator> + class transform_iterator_generator; + + template <class AdaptableUnaryFunction, class BaseIterator> + typename transform_iterator_generator<AdaptableUnaryFunction,Iterator>::type + make_transform_iterator(BaseIterator base, const AdaptableUnaryFunction& f = AdaptableUnaryFunction()); +} ++ +
+template <class AdaptableUnaryFunction, class Iterator> +class transform_iterator_generator +{ +public: + typedef iterator_adaptor<...> type; +}; ++ +
+The following is an example of how to use the +transform_iterator_generator class to iterate through a range of +numbers, multiplying each of them by 2 when they are dereferenced. + +
+
+#include <functional> +#include <iostream> +#include <boost/iterator_adaptors.hpp> + +int +main(int, char*[]) +{ + int x[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + + typedef std::binder1st< std::multiplies<int> > Function; + typedef boost::transform_iterator_generator<Function, int*>::type doubling_iterator; + + doubling_iterator i(x, std::bind1st(std::multiplies<int>(), 2)), + i_end(x + sizeof(x)/sizeof(int), std::bind1st(std::multiplies<int>(), 2)); + + std::cout << "multiplying the array by 2:" << std::endl; + while (i != i_end) + std::cout << *i++ << " "; + std::cout << std::endl; + + // to be continued... ++The output from this part is: +
+2 4 6 8 10 12 14 16 ++ +
Parameter | Description | +
---|---|
AdaptableUnaryFunction | +The function object that transforms each element in the iterator range. + |
BaseIterator | +The iterator type being wrapped. This type must at least be a model + of the InputIterator concept. | +
transform_iterator_generator::type(const BaseIterator& it, const AdaptableUnaryFunction& f = AdaptableUnaryFunction())+ +
+
+ + +
+template <class AdaptableUnaryFunction, class BaseIterator> +typename transform_iterator_generator<AdaptableUnaryFunction,BaseIterator>::type +make_transform_iterator(BaseIterator base, const AdaptableUnaryFunction& f = AdaptableUnaryFunction()); ++ +This function provides a convenient way to create transform iterators. + +
+ std::cout << "adding 4 to each element in the array:" << std::endl; + + std::copy(boost::make_transform_iterator(x, std::bind1st(std::plus+The output from this part is: +(), 4)), + boost::make_transform_iterator(x + N, std::bind1st(std::plus (), 4)), + std::ostream_iterator (std::cout, " ")); + std::cout << std::endl; + + return 0; +} +
+5 6 7 8 9 10 11 12 ++ +