1 // (C) Copyright Jeremy Siek 2000-2004.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
10 #include <boost/iterator/transform_iterator.hpp>
12 // What a bummer. We can't use std::binder1st with transform iterator
13 // because it does not have a default constructor. Here's a version
18 template <class Operation
>
20 : public std::unary_function
<typename
Operation::second_argument_type
,
21 typename
Operation::result_type
> {
24 typename
Operation::first_argument_type value
;
26 binder1st() { } // this had to be added!
27 binder1st(const Operation
& x
,
28 const typename
Operation::first_argument_type
& y
)
30 typename
Operation::result_type
31 operator()(const typename
Operation::second_argument_type
& x
) const {
36 template <class Operation
, class T
>
37 inline binder1st
<Operation
> bind1st(const Operation
& op
, const T
& x
) {
38 typedef typename
Operation::first_argument_type arg1_type
;
39 return binder1st
<Operation
>(op
, arg1_type(x
));
47 // This is a simple example of using the transform_iterators class to
48 // generate iterators that multiply the value returned by dereferencing
49 // the iterator. In this case we are multiplying by 2.
50 // Would be cooler to use lambda library in this example.
52 int x
[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
53 const int N
= sizeof(x
)/sizeof(int);
55 typedef boost::binder1st
< std::multiplies
<int> > Function
;
56 typedef boost::transform_iterator
<Function
, int*> doubling_iterator
;
58 doubling_iterator
i(x
, boost::bind1st(std::multiplies
<int>(), 2)),
59 i_end(x
+ N
, boost::bind1st(std::multiplies
<int>(), 2));
61 std::cout
<< "multiplying the array by 2:" << std::endl
;
63 std::cout
<< *i
++ << " ";
64 std::cout
<< std::endl
;
66 std::cout
<< "adding 4 to each element in the array:" << std::endl
;
68 std::copy(boost::make_transform_iterator(x
, boost::bind1st(std::plus
<int>(), 4)),
69 boost::make_transform_iterator(x
+ N
, boost::bind1st(std::plus
<int>(), 4)),
70 std::ostream_iterator
<int>(std::cout
, " "));
71 std::cout
<< std::endl
;