Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:13

0001 // name=thrust_find_index_of_first_occurence ; nvcc $name.cu -o /tmp/$name && /tmp/$name
0002 
0003 #include <thrust/device_vector.h>
0004 #include <stdio.h>
0005 
0006 struct less_than_or_eq_zero
0007 {
0008     __host__ __device__ bool operator() (double x) { return x <= 0.; }
0009 };
0010 
0011 int main(void)
0012 {
0013     int N = 6;
0014 
0015     thrust::device_vector<float> D(N);
0016 
0017     D[0] = 3.;
0018     D[1] = 2.3;
0019     D[2] = 1.3;
0020     D[3] = 0.1;
0021     D[4] = 3.;
0022     D[5] = 44.;
0023 
0024     thrust::device_vector<float>::iterator iter1 = D.begin();
0025     thrust::device_vector<float>::iterator iter2 = thrust::find_if(D.begin(), D.begin() + N, less_than_or_eq_zero());
0026     int d = thrust::distance(iter1, iter2);
0027 
0028     printf("Index = %i\n",d);  // when there are none returns index one past the end
0029 
0030     getchar();
0031 
0032     return 0;
0033 }
0034 
0035 /**
0036 
0037 
0038 
0039 C++ find indices of first occurence of multiple values
0040 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0041 
0042 * https://stackoverflow.com/questions/25846235/finding-the-indexes-of-all-occurrences-of-an-element-in-a-vector
0043 
0044 
0045 #include <algorithm> //find_if
0046 
0047 std::vector<int> A{1, 0, 1, 1, 0, 0, 0, 1, 0};
0048 std::vector<int> B;
0049 
0050 std::vector<int>::iterator it = A.begin();
0051 while ((it = std::find_if(it, A.end(), [](int x){return x == 0; })) != A.end())
0052 {
0053     B.push_back(std::distance(A.begin(), it));
0054     it++;
0055 }
0056 
0057 
0058 **/