Search code examples
c++cudaiteratorgpgputhrust

How to store the vector.begin() iterator of template type in thrust?


When I attempt to assign a variable to that iterator, i get the error: expected a ";", where vec is a thrust::device_vector<my_type>, j is some int, and my_type is a template type:

for (thrust::device_vector<my_type>::iterator i = vec.begin(); i < vec.end(); i += j) Foo(i);

Is this the correct way to loop over the vector? Am I declaring i as the correct type?


Solution

  • Standard containers use iterators for traversing through a collection of other objects (i.e. its elements), since iterator is abstract concept implemented in all standard containers, you can implement iterators in the following way:

    typename thrust::device_vector<my_type>::iterator it = vec.begin();
    for (it; it != vec.end(); it = it+j) 
       Foo(*it);
    

    Here's a reference to STL containers: http://www.cplusplus.com/reference/stl/