“锁定”两个向量和排序它们 [英] "Locking" two vectors and sorting them

查看:127
本文介绍了“锁定”两个向量和排序它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个向量< double> mass code>两个相同大小 N 。它们包含关于N个粒子的质量和速度的信息。 mass [i] velocity [i] 是第i个粒子的属性

I have this two vector<double> 's mass and velocity both of the same size N. They contain the information about the mass and velocity of N particles. mass[i] and velocity[i] are thus properties of the i'th particle

有可能在C ++中锁定这两个向量在一起,并按质量增加的顺序排序?
因此,在排序之后,向量 mass 应该按递增顺序,速度向量应该包含排序质量的相应速度

Is it possible in C++ to "lock" these two vectors together and sort them in increasing order of mass? 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 =(4,2,1,3)和velocity =(13,14,15,16)
之后排序mass =(1,2,3,4)和velocity =(15,14, 16,13)

e.g. 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)

我知道的一种(非高效)方式是将数据传递到struct的

The one (non-efficient) way I know for this is to transfer the data into an vector of struct's

struct particle
{

double mass;
double velocity;


bool operator < (const particle& str) const

 {
    return (mass < str.mass);
  }



};

并创建 vector< particle> particlelist(N),然后使用 std :: sort < 运算符,就像我在上面的定义中做的那样。

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.

我不想将我的数据放入结构体数组,因为我听说它比数组结构方法(至少在CUDA中)效率低下。

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

推荐答案

至少就我所知,标准库中内置的任何排序算法都不会直接为您执行此操作。最明显的可能性可能是使用Boost Zip迭代器使两个数组像单个集合一样。

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.

这篇关于“锁定”两个向量和排序它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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