Question

I would like to loop through two collections using iterators, modifying one based on a (sufficiently complex) algorithm involving the other. Consider the following minimal example:

#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/range/combine.hpp>
#include <boost/tuple/tuple.hpp> // tie
using namespace std;
using namespace boost;

int main(void) {
  // input data; never mind how these get filled
  int aa[] = {2, 3, 5, 8, 13, 21};
  int bb[] = {1, 0, 1, 1,  0,  1};
  vector<int> a (&aa[0], &aa[sizeof(aa)/sizeof(aa[0])]);
  vector<int> b (&bb[0], &bb[sizeof(bb)/sizeof(bb[0])]);

  // output storage; assume it has always correct dim.
  vector<int> c (a.size());

  // iterate through two coll., reading from both
  int p, q;
  BOOST_FOREACH (tie(p,q), combine(a,b)) { // loop1
    cout << p << "*" << q << "=" << p*q << endl;
  }

  // iterate through one coll., writing to it
  BOOST_FOREACH (int& r, c) { // loop2
    r = 42;
  }

  // iterate through two coll., reading from one, writing to the other?
  BOOST_FOREACH (??? p, s ???, combine(a,c)) { // loop3
    s = p * 2;
  }

  return 0;
}

How do I declare the part between the ???s (or otherwise change the parameters in loop3)?

Was it helpful?

Solution

The value type of a zip_range is a tuple of references to elements:

#include <iostream>
#include <vector>
#include <boost/range.hpp>
#include <boost/range/combine.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/foreach.hpp>

int main(int ac,char* av[])
{
  // input data; never mind how these get filled
  int aa[] = {2, 3, 5, 8, 13, 21};
  int bb[] = {1, 0, 1, 1,  0,  1};
  std::vector<int> a(boost::begin(aa), boost::end(aa));
  std::vector<int> const b(boost::begin(bb), boost::end(bb));

  // output storage; assume it has always correct dim.
  std::vector<int> c (a.size());

  typedef boost::tuple<int const&, int&> val_t;
  BOOST_FOREACH(val_t const& v, boost::combine(a, c)) {
    v.get<1>() = v.get<0>() * 2;
  }
}

OTHER TIPS

IMO, you're overusing BOOST_FOREACH to an almost shocking degree. Like std::foreach, this should be one of your last choices of algorithm.

Your third loop should almost certainly be written using std::transform. It's intended to take an input range, transform it, and deposit the result in an output range (or take two input ranges, combine them, and put the result in a third, such as in your first (mis)use of BOOST_FOREACH).

Using this, your third loop comes out something like:

// c[i] = a[i] * 2, i = 0..N-1
std::transform(begin(a), end(a), begin(c), [](int i) { return i * 2; });

As for your second, it looks like you really want std::fill_n.

std::fill_n(begin(c), end(c), 42);

Now, it's certainly true that something based on ranges (e.g., the algorithms in Boost Range) could make this a bit simpler by replacing each begin(X), end(X) pair with a single parameter. Nonetheless, these are already clearly superior to the BOOST_FOREACH versions with miscellaneous ties and combines to try force your square pegs into round holes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top