Boost logo

Boost Users :

Subject: [Boost-users] zip_iterator and sTL algorithms like random_shuffle
From: Andreas Fabri (andreas.fabri_at_[hidden])
Date: 2009-03-30 04:37:17


Hello,

I have two vectors and want to random_shuffle them the same way
using the zip_iterator. I thought the code below would do it,
but when I execute it I see that both sequences get screwded up.

What do I wrong?

Best regards,

andreas

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

int main()
{

   std::vector<int> I(8), J(8);
   for(int i = 0; i < I.size(); i++){
     I[i] = J[i] = i;
   }

   std::random_shuffle(boost::make_zip_iterator(boost::make_tuple(I.begin(), J.begin())),
                       boost::make_zip_iterator(boost::make_tuple(I.end(), J.end())));

   for(int i = 0; i < I.size(); i++){
     std::cout << I[i] << " " << J[i] << std::endl;
   }
   return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net