提高zip_iterator和std ::排序 [英] boost zip_iterator and std::sort

查看:157
本文介绍了提高zip_iterator和std ::排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组都是相同的长度。
我想排序逐键使用数组键阵列。
有人告诉我升压的拉链迭代器仅仅是合适的工具两个数组在一起,并在同一时间做的东西给他们。

下面是我在尝试使用升压:: zip_iterator解决问题的排序而失败, GCC 进行编译。有人可以帮助我解决这个问题code?

问题在于行

的std ::排序(升压:: make_zip_iterator(键,值),提振:: make_zip_iterator(键+ N,值+ N));

 的#include<&iostream的GT;
#包括LT&;&了iomanip GT;
#包括LT&; cstdlib>
#包括LT&;&的ctime GT;
#包括LT&;矢量>
#包括LT&;&算法GT;
#包括LT&;升压/迭代器/ zip_iterator.hpp>
#包括LT&;升压/元组/ tuple.hpp>
#包括LT&;升压/元组/ tuple_comparison.hpp>INT主(INT ARGC,CHAR *的argv [])
{
  INT N = 10;
  INT键[N];
  双值[N];
  INT M = 100;  //创建载体。
  的for(int i = 0; I< N ++ I)
   {
     键[I] =兰特()%M;
     值[I] = 1.0 * RAND()/ RAND_MAX;
   }
  //现在我们使用升压压缩迭代器压缩两个向量和排序他们simulatneously
  //我要排序逐键的键和值阵列
   的std ::排序(升压:: make_zip_iterator(键,值)
               提高:: make_zip_iterator(键+ N,值+ N)
             );
    //值数组,按升序排列相应的键。
   的for(int i = 0; I< N ++ I)
    {
      性病::法院LT&;<键[1] - ;&下; \\ t的<<值[1] - ;&下;的std :: ENDL;
     }
  返回0;
}

注:编译错误信息

  g ++的-g -Wall boost_test.cpp
boost_test.cpp:在函数'主INT(INT,CHAR **):
boost_test.cpp:37:56:错误:调用'make_zip_iterator(INT [(((无符号整数)(((INT)N)+ -0x00000000000000001))+ 1)]没有匹配的功能,双[(((无符号INT)(((int)的N)+ -0x00000000000000001))+ 1)])'
boost_test.cpp:38:64:错误:调用没有匹配的函数make_zip_iterator为(int *,双*)'


解决方案

您无法排序一双zip_iterators的。

首先,make_zip_iterator采用迭代器作为输入的元组,所以你可以拨打:

 的boost :: make_zip_iterator(升压:: make_tuple(...))

但不会编译要么,因为键+ N 不具有相同的类型。我们需要强制成为一个指针:

 的std ::排序(升压:: make_zip_iterator(升压:: make_tuple(+按键+值))
          提高:: make_zip_iterator(升压:: make_tuple(键+ N,值+ N)));

这将编译,但是排序的结果仍然是错误的,因为只有zip_iterator机型<一个href=\"http://www.boost.org/doc/libs/1_48_0/libs/iterator/doc/zip_iterator.html#zip-iterator-models\">Readable迭代器,但的std ::排序也需要输入的是<一个href=\"http://www.boost.org/doc/libs/1_48_0/libs/iterator/doc/new-iter-concepts.html#writable-iterators-lib-writable-iterators\">Writable为<一个href=\"http://www.boost.org/doc/libs/1_48_0/libs/iterator/doc/new-iter-concepts.html#changes-to-algorithm-requirements\">described这里,所以你不能使用排序zip_iterator。

I have two arrays values and keys both of the same length. I want to sort-by-key the values array using the keys array as keys. I have been told the boost's zip iterator is just the right tool for locking two arrays together and doing stuff to them at the same time.

Here is my attempt at using the boost::zip_iterator to solve sorting problem which fails to compile with gcc. Can someone help me fix this code?

The problem lies in the line

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

NOTE:Error message on compilation

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*)’

解决方案

You can't sort a pair of zip_iterators.

Firstly, make_zip_iterator takes a tuple of iterators as input, so you could call:

boost::make_zip_iterator(boost::make_tuple( ... ))

but that won't compile either, because keys and keys+N doesn't have the same type. We need to force keys to become a pointer:

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

this will compile, but the sorted result is still wrong, because a zip_iterator only models a Readable iterator, but std::sort also needs the input to be Writable as described here, so you can't sort using zip_iterator.

这篇关于提高zip_iterator和std ::排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆