有没有一种方法可以使用 Thrust 根据索引向量设置标志
Is there a way to use Thrust to set flag based on vector of indices
假设我有一个数组 A[0,2,4,5,6,7]
我要转成B:
B [1,0,1,0,1,1,1,1]
所以A表示B中需要设置为1的元素的索引,A已经排序。
已知数组A中的最大数,从而预先知道B的大小。
是否有调用 Thrust 库来完成此操作的简单方法?
感谢您的帮助
这可以在一行中完成:
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/copy.h>
#include <iostream>
int main()
{
int A[6] = {0,2,4,5,6,7};
int B[7] = {0};
const int count_A = 6;
thrust::copy(thrust::make_constant_iterator(1), thrust::make_constant_iterator(1)+count_A, thrust::make_permutation_iterator(B,A));
thrust::copy(B, B+7, std::ostream_iterator<int>(std::cout, " "));
return 0;
}
输出:
1 0 1 0 1 1 1
假设我有一个数组 A[0,2,4,5,6,7]
我要转成B:
B [1,0,1,0,1,1,1,1]
所以A表示B中需要设置为1的元素的索引,A已经排序。
已知数组A中的最大数,从而预先知道B的大小。
是否有调用 Thrust 库来完成此操作的简单方法?
感谢您的帮助
这可以在一行中完成:
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/copy.h>
#include <iostream>
int main()
{
int A[6] = {0,2,4,5,6,7};
int B[7] = {0};
const int count_A = 6;
thrust::copy(thrust::make_constant_iterator(1), thrust::make_constant_iterator(1)+count_A, thrust::make_permutation_iterator(B,A));
thrust::copy(B, B+7, std::ostream_iterator<int>(std::cout, " "));
return 0;
}
输出:
1 0 1 0 1 1 1