2

当我尝试将变量分配给该迭代器时,我收到错误:expected a ";",其中vec是 a thrust::device_vector<my_type>j是 some int,并且my_type是模板类型:

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

这是循环向量的正确方法吗?我声明i为正确的类型吗?

4

1 回答 1

1

标准容器使用迭代器来遍历其他对象(即其元素)的集合,由于迭代器是在所有标准容器中实现的抽象概念,您可以通过以下方式实现迭代器:

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

这是对 STL 容器的参考:http ://www.cplusplus.com/reference/stl/

于 2013-05-05T01:41:49.930 回答