diff --git a/transform_iterator.htm b/transform_iterator.htm index 3fd9393..e0f0b02 100644 --- a/transform_iterator.htm +++ b/transform_iterator.htm @@ -71,8 +71,11 @@ public:
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. +transform_iterator_generator class to iterate through a range +of numbers, multiplying each of them by 2 when they are dereferenced. +The boost::binder1st class is used instead of the standard +one because tranform iterator requires the function object to be +Default Constructible.
@@ -80,16 +83,18 @@ numbers, multiplying each of them by 2 when they are dereferenced. #include <iostream> #include <boost/iterator_adaptors.hpp> +// definition of class boost::binder1st and function boost::bind1st() ... + int main(int, char*[]) { int x[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; - typedef std::binder1st< std::multiplies<int> > Function; + typedef boost::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)); + doubling_iterator i(x, boost::bind1st(std::multiplies<int>(), 2)), + i_end(x + sizeof(x)/sizeof(int), boost::bind1st(std::multiplies<int>(), 2)); std::cout << "multiplying the array by 2:" << std::endl; while (i != i_end) @@ -111,14 +116,17 @@ The output from this part is:- AdaptableUnaryFunction +AdaptableUnaryFunction The function object that transforms each element in the iterator range. The argument_type of the function object must match the value type of the base iterator. The result_type of the function object will be the resulting iterator's value_type. If you want the resulting iterator to behave as an iterator, the result of the function should be solely a function of -its argument. +its argument. Also, the function object must be Default +Constructible (which many of the standard function objects are not).@@ -173,8 +181,8 @@ function to add four to each element of the array. std::cout << "adding 4 to each element in the array:" << std::endl; - std::copy(boost::make_transform_iterator(x, std::bind1st(std::plus(), 4)), - boost::make_transform_iterator(x + N, std::bind1st(std::plus (), 4)), + std::copy(boost::make_transform_iterator(x, boost::bind1st(std::plus (), 4)), + boost::make_transform_iterator(x + N, boost::bind1st(std::plus (), 4)), std::ostream_iterator (std::cout, " ")); std::cout << std::endl;