Boost logo

Boost :

From: Alex Mendes da Costa (alexmdac_at_[hidden])
Date: 2005-07-26 03:01:36


On 7/25/05, David Abrahams <dave_at_[hidden]> wrote:
> Why not use zip_iterator?

zip_iterator doesn't do what I want. From the documentation:

"The zip iterator provides the ability to parallel-iterate over
several controlled sequences simultaneously. A zip iterator is
constructed from a tuple of iterators. Moving the zip iterator moves
all the iterators in parallel. Dereferencing the zip iterator returns
a tuple that contains the results of dereferencing the individual
iterators."

For example, if I had two sequences {1,2} and {10,20} and I used a
zip_iterator to iterate through them, I would get (1,10) and (2,20).
This can be verified by compiling and executing the following short
program:

#include <boost/iterator/zip_iterator.hpp>
#include <boost/tuple/tuple.hpp>
#include <iostream>
#include <vector>

int main() {
  typedef std::vector<int> IntSeq;
  
  // a = {1,2}
  IntSeq a;
  a.push_back(1);
  a.push_back(2);

  // b = {10,20}
  IntSeq b;
  b.push_back(10);
  b.push_back(20);
  
  typedef boost::zip_iterator<
    boost::tuples::tuple<
      IntSeq::const_iterator,
      IntSeq::const_iterator> > ZIt;

  ZIt cur(boost::make_tuple(a.begin(), b.begin()));
  ZIt end(boost::make_tuple(a.end(), b.end()));

  for ( ; cur != end; ++cur ) {
    std::cout << "{ " << boost::tuples::get<0>(*cur) << ", "
                      << boost::tuples::get<1>(*cur) << " }" << std::endl;
  }

  return 0;
}

The iterator that I am proposing would provide the sequence (1,10),
(2,10), (1,20), (2,20).

Alex


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk