简体   繁体   English

“锁定”两个向量并对它们进行排序

[英]“Locking” two vectors and sorting them

I have this two vector<double> 's mass and velocity both of the same size N . 我有两个vector<double>massvelocity都是相同大小的N They contain the information about the mass and velocity of N particles. 它们包含有关N粒子质量和速度的信息。 mass[i] and velocity[i] are thus properties of the i'th particle mass[i]velocity[i]因此是第i个粒子的属性

Is it possible in C++ to "lock" these two vectors together and sort them in increasing order of mass? 在C ++中是否有可能将这两个向量“锁定”在一起并按质量递增顺序对它们进行排序? Thus after the sorting the vector mass should be in increasing order, and the velocity vector should contain the corresponding velocities of the sorted masses 因此,在排序之后,矢量mass应该是递增的顺序,并且速度矢量应该包含排序的质量的相应速度

eg Before sorting mass = (4,2,1,3) and velocity = (13, 14,15,16 ) After sorting mass=(1,2,3,4) and velocity =(15, 14, 16, 13) 例如,在排序质量=(4,2,1,3)和速度=(13,14,15,16)之后排序质量=(1,2,3,4)和速度=(15,14,16,13) )

The one (non-efficient) way I know for this is to transfer the data into an vector of struct's 我知道的一种(非高效)方式是将数据传输到struct的向量中

struct particle
{

double mass;
double velocity;


bool operator < (const particle& str) const

 {
    return (mass < str.mass);
  }



};

and create vector<particle> particlelist(N) and then sort this vector by using the std::sort by overloading the < operator as I have done in the definition above. 并创建vector<particle> particlelist(N) ,然后通过使用std::sort对这个向量进行std::sort是重载<运算符,就像我在上面的定义中所做的那样。

I do not want to put my data into Array of Structures fashion since I have heard it is inefficient compared to the Structure of Arrays approach(at least in CUDA). 我不想把我的数据放入Array of Structures时尚,因为我听说它与阵列结构方法相比效率低(至少在CUDA中)。

Create vector indexes; 创建矢量索引; fill it by values 0..n-1 than 用值0..n-1填充它

    struct CmpMass {
    {
       CmpMass(vector<double>& vec) : values(vec){}
       bool operator() (const int& a, const int& b) const
       {
           return values[a] < values[b];
       }
       vector<double>& values;
    }

sort(indexes.begin(), indexes.end(), CmpMass(mass));

than you have in vector indexes order of items in both arrays. 比你在矢量索引中的两个数组中的项目顺序。 Than you can create mass/velocity vectors in correct order or convert index during access: mass[indexes[i]], velocity[indexes[i]] 您可以在正确的顺序中创建质量/速度矢量或在访问期间转换索引:mass [indices [i]],velocity [indices [i]]

At least as far as I know, none of the sorting algorithms built into the standard library will do this for you directly. 至少据我所知,标准库中内置的排序算法都不会直接为您完成。 The most obvious possibility would probably be to use a Boost Zip Iterator to make the two arrays act like a single collection. 最明显的可能性可能是使用Boost Zip Iterator使两个数组像一个集合。

Why don't you use std::pair as you have two values which are linked, you than can then implement your own comparison method/function to pass on to the std::sort function via pointer(there exist an overloaded version of std::sort which supports that). 你为什么不使用std::pair因为你有两个链接的值,然后你可以实现自己的比较方法/函数通过指针传递给std::sort函数(存在std::sort的重载版本) std::sort支持那个)。

But be sure that you have a strict weak ordering implemented, because else std::sort might lead to a SEGFAULT 但请确保您实现了严格的弱排序 ,因为否则std::sort可能会导致SEGFAULT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM