<bdo id='y9CRj'></bdo><ul id='y9CRj'></ul>
<tfoot id='y9CRj'></tfoot>
  • <small id='y9CRj'></small><noframes id='y9CRj'>

  • <legend id='y9CRj'><style id='y9CRj'><dir id='y9CRj'><q id='y9CRj'></q></dir></style></legend>

        <i id='y9CRj'><tr id='y9CRj'><dt id='y9CRj'><q id='y9CRj'><span id='y9CRj'><b id='y9CRj'><form id='y9CRj'><ins id='y9CRj'></ins><ul id='y9CRj'></ul><sub id='y9CRj'></sub></form><legend id='y9CRj'></legend><bdo id='y9CRj'><pre id='y9CRj'><center id='y9CRj'></center></pre></bdo></b><th id='y9CRj'></th></span></q></dt></tr></i><div id='y9CRj'><tfoot id='y9CRj'></tfoot><dl id='y9CRj'><fieldset id='y9CRj'></fieldset></dl></div>
      1. 提升 zip_iterator 和 std::sort

        boost zip_iterator and std::sort(提升 zip_iterator 和 std::sort)
        1. <small id='pPMd9'></small><noframes id='pPMd9'>

          • <bdo id='pPMd9'></bdo><ul id='pPMd9'></ul>
            <tfoot id='pPMd9'></tfoot>

                  <tbody id='pPMd9'></tbody>

                <i id='pPMd9'><tr id='pPMd9'><dt id='pPMd9'><q id='pPMd9'><span id='pPMd9'><b id='pPMd9'><form id='pPMd9'><ins id='pPMd9'></ins><ul id='pPMd9'></ul><sub id='pPMd9'></sub></form><legend id='pPMd9'></legend><bdo id='pPMd9'><pre id='pPMd9'><center id='pPMd9'></center></pre></bdo></b><th id='pPMd9'></th></span></q></dt></tr></i><div id='pPMd9'><tfoot id='pPMd9'></tfoot><dl id='pPMd9'><fieldset id='pPMd9'></fieldset></dl></div>
              1. <legend id='pPMd9'><style id='pPMd9'><dir id='pPMd9'><q id='pPMd9'></q></dir></style></legend>
                  本文介绍了提升 zip_iterator 和 std::sort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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.

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

                  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?

                  问题出在线路上

                  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]   <<  "	"  << values[i]    << std::endl;  
                       }
                    return 0;
                  }
                  

                  注意:编译错误信息

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

                  推荐答案

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

                  You can't sort a pair of zip_iterators.

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

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

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

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

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

                  这会编译,但排序结果仍然是错误的,因为 zip_iterator 只对 可读迭代器,但 std::sort 也需要输入为 Writable 作为 此处描述,因此您无法使用 zip_iterator 进行排序.

                  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::sort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)

                    <i id='3QKh0'><tr id='3QKh0'><dt id='3QKh0'><q id='3QKh0'><span id='3QKh0'><b id='3QKh0'><form id='3QKh0'><ins id='3QKh0'></ins><ul id='3QKh0'></ul><sub id='3QKh0'></sub></form><legend id='3QKh0'></legend><bdo id='3QKh0'><pre id='3QKh0'><center id='3QKh0'></center></pre></bdo></b><th id='3QKh0'></th></span></q></dt></tr></i><div id='3QKh0'><tfoot id='3QKh0'></tfoot><dl id='3QKh0'><fieldset id='3QKh0'></fieldset></dl></div>

                  • <legend id='3QKh0'><style id='3QKh0'><dir id='3QKh0'><q id='3QKh0'></q></dir></style></legend>
                      <bdo id='3QKh0'></bdo><ul id='3QKh0'></ul>
                        <tbody id='3QKh0'></tbody>

                        <small id='3QKh0'></small><noframes id='3QKh0'>

                            <tfoot id='3QKh0'></tfoot>