要从数组中删除某些元素,您可以使用Thrust Library的压缩操作.给定一个谓词is_not_zero
,它返回false
零值,true
对于其他谓词,你可以写这样的操作
thrust::copy_if(in_array, in_array + size, out_array, is_not_zero);
Run Code Online (Sandbox Code Playgroud)
输出数组将仅包含非零值,因为谓词表示如此.
您也可以将"remove_if"函数与反向谓词一起使用,该反向谓词返回true
零,false
对于其他人.
thrust::remove_if(in_array, in_array + size, is_zero);
Run Code Online (Sandbox Code Playgroud)
我建议你看一下Thrust库的压缩例子,或者一般的压缩概念.
http://code.google.com/p/thrust/source/browse/examples/stream_compaction.cu