C ++操作在“打包”数组 [英] C++ operating on "packed" arrays

查看:90
本文介绍了C ++操作在“打包”数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个,例如 float 数组 a b int 键数组 k 和模板 mySortByKey 我自己的函数,在单个数组上操作,像

Suppose that I have two, for example, float arrays a and b, an int key array k and a template mySortByKey function of my own, operating on a single array, something like

template<class T>
mySortByKey(int *k, T *a)

使用zip迭代器和某些类型的元组)使 mySort 同时在 a b c

Is there a possibility (for example, using zip iterators and tuples of some sorts) to enable mySort operating simultaneously on a and b, so that they can be simultaneously ordered according to the key k?

推荐答案

我不认为你可以这样做。但是,您可以使用辅助数组的索引来完成类似的操作。

I don't think you can do that. However, you can accomplish something similar by the use of a helper array of indices.

int keys[ARRAY_SIZE];
float a[ARRAY_SIZE];
float b[ARRAY_SIZE];

// Fill up the contents of keys, a, and b

// Create an array of indices.
int indices[ARRAY_SIZE];
for ( int i = 0; i < ARRAY_SIZE; ++i )
   indices[i] = i;

// Sort the indices using keys.
mySortByKey(keys, indices);

// Now access the arrays a and b indirectly, using the sorted array
// of indices as an intermediate object.
for ( int i = 0; i < ARRAY_SIZE; ++i )
{
   float fa = a[indices[i]];
   float fb = b[indices[i]];
}

这篇关于C ++操作在“打包”数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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