You can do this using thrust as @RobertCrovella already pointed out.
The following example uses thrust::copy_if
to copy all of elements’ indices for which the condition (“equals 7”) is fulfilled. thrust::counting_iterator
is used to avoid creating the sequence of indices explicitly.
#include <thrust/copy.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/functional.h>
#include <iostream>
using namespace thrust::placeholders;
int main()
{
const int N = 10;
int objectArray[N] = { 1, 11, 7, 2, 7, 23, 6, 6, 9, 11 };
int results[N]={0};
int* end = thrust::copy_if(thrust::make_counting_iterator(0), thrust::make_counting_iterator(N), objectArray, results, _1 == 7);
thrust::copy(results, results+N, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl << "result count = " << end-results << std::endl;
return 0;
}
Output:
2 4 0 0 0 0 0 0 0 0
result count = 2
3
solved Cuda: Compact and result size