1 .. Copyright David Abrahams 2006. Distributed under the Boost
2 .. Software License, Version 1.0. (See accompanying
3 .. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 There are two main types of applications of the ``zip_iterator``. The first
9 one concerns runtime efficiency: If one has several controlled sequences
10 of the same length that must be somehow processed, e.g., with the
11 ``for_each`` algorithm, then it is more efficient to perform just
12 one parallel-iteration rather than several individual iterations. For an
13 example, assume that ``vect_of_doubles`` and ``vect_of_ints``
14 are two vectors of equal length containing doubles and ints, respectively,
15 and consider the following two iterations:
20 std::vector<double>::const_iterator beg1 = vect_of_doubles.begin();
21 std::vector<double>::const_iterator end1 = vect_of_doubles.end();
22 std::vector<int>::const_iterator beg2 = vect_of_ints.begin();
23 std::vector<int>::const_iterator end2 = vect_of_ints.end();
25 std::for_each(beg1, end1, func_0());
26 std::for_each(beg2, end2, func_1());
28 These two iterations can now be replaced with a single one as follows:
34 boost::make_zip_iterator(
35 boost::make_tuple(beg1, beg2)
37 boost::make_zip_iterator(
38 boost::make_tuple(end1, end2)
43 A non-generic implementation of ``zip_func`` could look as follows:
49 public std::unary_function<const boost::tuple<const double&, const int&>&, void>
51 void operator()(const boost::tuple<const double&, const int&>& t) const
62 The second important application of the ``zip_iterator`` is as a building block
63 to make combining iterators. A combining iterator is an iterator
64 that parallel-iterates over several controlled sequences and, upon
65 dereferencing, returns the result of applying a functor to the values of the
66 sequences at the respective positions. This can now be achieved by using the
67 ``zip_iterator`` in conjunction with the ``transform_iterator``.
69 Suppose, for example, that you have two vectors of doubles, say
70 ``vect_1`` and ``vect_2``, and you need to expose to a client
71 a controlled sequence containing the products of the elements of
72 ``vect_1`` and ``vect_2``. Rather than placing these products
73 in a third vector, you can use a combining iterator that calculates the
74 products on the fly. Let us assume that ``tuple_multiplies`` is a
75 functor that works like ``std::multiplies``, except that it takes
76 its two arguments packaged in a tuple. Then the two iterators
77 ``it_begin`` and ``it_end`` defined below delimit a controlled
78 sequence containing the products of the elements of ``vect_1`` and
85 std::vector<double>::const_iterator,
86 std::vector<double>::const_iterator
89 typedef boost::zip_iterator<
93 typedef boost::transform_iterator<
94 tuple_multiplies<double>,
96 > the_transform_iterator;
98 the_transform_iterator it_begin(
105 tuple_multiplies<double>()
108 the_transform_iterator it_end(
115 tuple_multiplies<double>()