提升zip_iterator和std :: sort

cur*_*rer 11 c++ boost

我有两个数组values,keys两个长度相同.我想values使用keys数组作为键对数组进行排序.我被告知boost的zip迭代器只是将两个数组锁定在一起并同时对它们进行处理的正确工具.

这是我尝试使用boost :: zip_iterator来解决无法编译的排序问题gcc.有人可以帮我修复这段代码吗?

问题在于线

std::sort ( boost::make_zip_iterator( keys, values ), boost::make_zip_iterator( keys+N , values+N ));

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <boost/iterator/zip_iterator.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>



int main(int argc, char *argv[])
{
  int N=10;
  int    keys[N];
  double values[N];
  int M=100;

  //Create the vectors.
  for (int i = 0; i < N; ++i)
   {
     keys[i]   = rand()%M;
     values[i] = 1.0*rand()/RAND_MAX;
   }


  //Now we use the boost zip iterator to zip the two vectors and sort them "simulatneously"
  //I want to sort-by-key the keys and values arrays
   std::sort ( boost::make_zip_iterator( keys, values  ), 
               boost::make_zip_iterator( keys+N  , values+N    )
             );
    //The values array and the corresponding keys in ascending order. 
   for (int i = 0; i < N; ++i)
    {
      std::cout << keys[i]   <<  "\t"  << values[i]    << std::endl;  
     }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

注意:编译时出现错误消息

g++ -g -Wall boost_test.cpp 
boost_test.cpp: In function ‘int main(int, char**)’:
boost_test.cpp:37:56: error: no matching function for call to ‘make_zip_iterator(int [(((unsigned int)(((int)N) + -0x00000000000000001)) + 1)], double [(((unsigned int)(((int)N) + -0x00000000000000001)) + 1)])’
boost_test.cpp:38:64: error: no matching function for call to ‘make_zip_iterator(int*, double*)’
Run Code Online (Sandbox Code Playgroud)

ken*_*ytm 11

你不能对一对zip_iterator进行排序.

首先,make_zip_iterator将迭代器元组作为输入,因此您可以调用:

boost::make_zip_iterator(boost::make_tuple( ... ))
Run Code Online (Sandbox Code Playgroud)

但是也不会编译,因为keys并且keys+N没有相同的类型.我们需要强制keys成为一个指针:

std::sort(boost::make_zip_iterator(boost::make_tuple(+keys, +values)),
          boost::make_zip_iterator(boost::make_tuple(keys+N, values+N)));
Run Code Online (Sandbox Code Playgroud)

这将编译,但排序结果仍然是错误的,因为只有zip_iterator车型可读迭代器,但std::sort也需要输入视为可写这里描述的,所以你不能使用排序zip_iterator.


Car*_*ook 5

关于这个问题的一个很好的讨论可以在这里找到:https : //web.archive.org/web/20120422174751/http : //www.stanford.edu/~dgleich/notebook/2006/03/sorting_two_arrays_simultaneou.html

这是这个问题的可能重复:Sorting zipped (locked) containers in C++ using boost or the STL

上面链接中的方法使用 std::sort,没有额外的空间。它不使用 boost::zip_iterator,只使用 boost 元组和 boost 迭代器外观。如果您有最新的编译器,Std::tuples 也应该可以工作。

如果您很高兴有一个额外的向量(由 size_t 个元素组成),那么以下方法将适用于 ~ o(n log n) 时间平均情况。这相当简单,但是如果您搜索它们,将会有更好的方法。

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

template <typename T1, typename T2>
void sortByPerm(vector<T1>& list1, vector<T2>& list2) {
  const auto len = list1.size();
  if (!len || len != list2.size()) throw;

  // create permutation vector
  vector<size_t> perms;
  for (size_t i = 0; i < len; i++) perms.push_back(i);
  sort(perms.begin(), perms.end(), [&](T1 a, T1 b){ return list1[a] < list1[b]; });

  // order input vectors by permutation
  for (size_t i = 0; i < len - 1; i++) {
    swap(list1[i], list1[perms[i]]);
    swap(list2[i], list2[perms[i]]);

    // adjust permutation vector if required
    if (i < perms[i]) {
      auto d = distance(perms.begin(), find(perms.begin() + i, perms.end(), i));
      swap(perms[i], perms[d]);
    }
  }
}

int main() {
  vector<int> ints = {32, 12, 40, 8, 9, 15};
  vector<double> doubles = {55.1, 33.3, 66.1, 11.1, 22.1, 44.1};

  sortByPerm(ints, doubles);   

  copy(ints.begin(), ints.end(), ostream_iterator<int>(cout, " ")); cout << endl;
  copy(doubles.begin(), doubles.end(), ostream_iterator<double>(cout, " ")); cout << endl;
}
Run Code Online (Sandbox Code Playgroud)