zip_iterator and sTL algorithms like random_shuffle

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; }

Hi Steven, Adding a specialization for iter_swap typedef boost::tuple< std::vector<int>::iterator, std::vector<int>::iterator
iterator_tuple;
typedef boost::zip_iterator< iterator_tuple
ziterator;
namespace std { void iter_swap(ziterator a, ziterator b) { std::swap(a->get<0>(), b->get<0>()); std::swap(a->get<1>(), b->get<1>()); } } solved the problem for std::random_shuffle. For std::sort I provide a comparison functor for the tuple. but the compiler (VC8) complains about no match for _Rotate Any hint would be appreciated. andreas C:\Program Files (x86)\Microsoft Visual Studio 8\VC\INCLUDE\algorithm(1719) : error C2665: 'std::_Rotate' : none of the 3 overloads could convert all the argument types C:\Program Files (x86)\Microsoft Visual Studio 8\VC\INCLUDE\algorithm(1649): could be 'void std::_Rotate<_FwdIt>(_FwdIt,_FwdIt,_FwdIt,std::forward_iterator_tag)' with [ _FwdIt=boost::zip_iterator<iterator_tuple> ] C:\Program Files (x86)\Microsoft Visual Studio 8\VC\INCLUDE\algorithm(1666): or 'void std::_Rotate<_FwdIt>(_BidIt,_BidIt,_BidIt,std::bidirectional_iterator_tag)' with [ _FwdIt=boost::zip_iterator<iterator_tuple>, _BidIt=boost::zip_iterator<iterator_tuple> ] C:\Program Files (x86)\Microsoft Visual Studio 8\VC\INCLUDE\algorithm(1709): or 'void std::_Rotate<_FwdIt>(_RanIt,_RanIt,_RanIt,std::random_access_iterator_tag)' with [ _FwdIt=boost::zip_iterator<iterator_tuple>, _RanIt=boost::zip_iterator<iterator_tuple> ] while trying to match the argument list '(boost::zip_iterator<IteratorTuple>, boost::zip_iterator<IteratorTuple>, boost::zip_iterator<IteratorTuple>, boost::detail::iterator_category_with_traversal<Category,Traversal>)' with [ IteratorTuple=iterator_tuple ] and [ IteratorTuple=iterator_tuple ] and [ IteratorTuple=iterator_tuple ] and [ Category=std::input_iterator_tag, Traversal=boost::random_access_traversal_tag ] C:\Program Files (x86)\Microsoft Visual Studio 8\VC\INCLUDE\algorithm(3125) : see reference to function template instantiation 'void std::rotate<_BidIt>(_FwdIt,_FwdIt,_FwdIt)' being compiled with [ _BidIt=boost::zip_iterator<iterator_tuple>, _FwdIt=boost::zip_iterator<iterator_tuple> ] C:\Program Files (x86)\Microsoft Visual Studio 8\VC\INCLUDE\algorithm(3252) : see reference to function template instantiation 'void std::_Insertion_sort<_RanIt,_Pr>(_BidIt,_BidIt,_Pr)' being compiled with [ _RanIt=boost::zip_iterator<iterator_tuple>, _Pr=Compare, _BidIt=boost::zip_iterator<iterator_tuple> ] C:\Program Files (x86)\Microsoft Visual Studio 8\VC\INCLUDE\algorithm(3261) : see reference to function template instantiation 'void std::_Sort<_RanIt,__int64,_Pr>(_RanIt,_RanIt,_Diff,_Pr)' being compiled with [ _RanIt=boost::zip_iterator<iterator_tuple>, _Pr=Compare, _Diff=__int64 ] Steven Watanabe wrote:

This is not standard conform to introduce overloaded functions into std namespace. This posting might answer your question: http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/b39... It is pretty long, but worth reading it all. And that is what Steven means in his previous post, that there is no C++03 standard way to fix it. Greetings, Ovanes On Tue, Mar 31, 2009 at 12:59 PM, Andreas Fabri < andreas.fabri@geometryfactory.com> wrote:

AMDG Ovanes Markarian wrote:
Note that a trivial change will make this special case correct: inline void iter_swap<>(ziterator a, ziterator b) because specialization of std library templates is allowed. However, this doesn't work in general because function templates can't be partially specialized. In Christ, Steven Watanabe
participants (3)
-
Andreas Fabri
-
Ovanes Markarian
-
Steven Watanabe