当我尝试将变量分配给该迭代器时,我收到错误:expected a ";"
,其中vec
是 thrust::device_vector<my_type>
, j
是一些int
,和my_type
是一个模板类型:
for (thrust::device_vector<my_type>::iterator i = vec.begin(); i < vec.end(); i += j) Foo(i);
这是循环 vector 的正确方法吗?我是否声明i
作为正确的类型?
最佳答案
标准容器使用迭代器来遍历其他对象(即其元素)的集合,由于迭代器是所有标准容器中实现的抽象概念,因此可以通过以下方式实现迭代器:
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/
关于c++ - 如何将模板类型的vector.begin()迭代器存储在thrust中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380464/