C++在"打包"阵列上运行
C++ operating on "packed" arrays
假设我有两个,例如float
数组a
和b
,一个int
密钥数组k
和一个我自己的模板mySortByKey
函数,在一个数组上操作,类似
template<class T>
mySortByKey(int *k, T *a)
是否有可能(例如,使用某些类型的zip迭代器和元组)启用mySort
同时在a
和b
上操作,以便它们可以根据密钥k
同时排序?
我认为你做不到。但是,您可以通过使用索引的辅助数组来实现类似的功能。
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]];
}
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- OpenMP阵列性能较差
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 简单的程序比较阵列运行但给出不正确的结果
- 获得0作为分配的阵列的大小,其大小在运行时给出
- 删除指向排序的字符串阵列的指针会在运行后会有一个错误
- 可解析的阵列和摊销运行时
- C 将指针的阵列运行
- 如何使动态数组或矢量以与标准阵列相似的速度运行?C++
- C++在"打包"阵列上运行