相关疑难解决方法(0)

使用boost或STL在C++中对压缩(锁定)容器进行排序

我想做什么:我想将2个,3个或N个向量锁定在一起,而不是将它们复制到元组中.也就是说,将冗长放在一边,例如:

vector<int>    v1 = {  1,   2,   3,   4,   5};
vector<double> v2 = { 11,  22,  33,  44,  55};
vector<long>   v3 = {111, 222, 333, 444, 555};

typedef tuple<int&,double&,long&> tup_t;
sort(zip(v1,v2,v3),[](tup_t t1, tup_t t2){ return t1.get<0>() > t2.get<0>(); });

for(auto& t : zip(v1,v2,v3))
  cout << t.get<0>() << " " << t.get<1>() << " " << t.get<2>() << endl;
Run Code Online (Sandbox Code Playgroud)

这应输出:

5 55 555
4 44 444
...
1 11 111
Run Code Online (Sandbox Code Playgroud)

我现在是怎么做的:我已经实现了自己的快速排序,我传递的第一个数组用于比较,排列应用于所有其他数组.我只是无法弄清楚如何重用std :: sort来解决我的问题(例如提取排列).

我试过的: boost :: zip_iterator …

c++ algorithm boost std c++11

44
推荐指数
3
解决办法
3482
查看次数

提升zip_iterator和std :: sort

我有两个数组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;
   } …
Run Code Online (Sandbox Code Playgroud)

c++ boost

11
推荐指数
2
解决办法
6245
查看次数

标签 统计

boost ×2

c++ ×2

algorithm ×1

c++11 ×1

std ×1