Boost logo

Boost Users :

Subject: [Boost-users] [Iterator] swap and iter_swap do not work with zip_iterator
From: Benjamin Lindley (benjameslindley_at_[hidden])
Date: 2012-07-27 13:59:04


I wanted to use zip_iterator to concurrently sort two arrays by the
values in one of them. It didn't work. Most of the values were
over-written by one of the pairs. I figured out that there was a
problem with the swapping, so I made this small test case. Compiled
with GCC 4.6.1 on Ubuntu 11.10

#include <utility>
#include <iostream>

#include <boost/iterator/zip_iterator.hpp>

int main()
{
     std::vector<int> a = { 1, 2 };
     std::vector<int> b = { 100, 200 };
     
     auto first = boost::make_zip_iterator(
         boost::make_tuple(a.begin(), b.begin()));
     auto last = boost::make_zip_iterator(
         boost::make_tuple(a.begin() + 1, b.begin() + 1));
     
     std::cout << first->get<0>() << ' ' << last->get<0>() << '\n';
     std::cout << first->get<1>() << ' ' << last->get<1>() << "\n\n";
     
     using std::swap;
     using std::iter_swap;
     // swap(*first, *last); // doesn't compile
     iter_swap(first, last);
     
     std::cout << first->get<0>() << ' ' << last->get<0>() << '\n';
     std::cout << first->get<1>() << ' ' << last->get<1>() << "\n\n";
}

Output:

1 2
100 200

2 2
200 200

Expected Output:

1 2
100 200

2 1
200 100

Is this a known problem?


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