|
使用zip_iterator迭代器、make_tuple、thrust::get<0>
// 1、stl的容器都默认会有迭代器的 通用的
// 2、不同容器的迭代器可以组合成一个元组 thrust::tuple
// 3、迭代器元组可以被封装成zip_iterator thrust::zip_iterator
// 4、zip_iterator的初始化 thrust::make_tuple
// 5、最终我们使用的就是一个zip_iterator的迭代器对象,像使用stl中的普通迭代器一样
// 6、通过zip_iterator取值 thrust::get<1>(zip_iter[2])
- #include <thrust/device_vector.h>
- #include <iostream>
- #include <thrust/iterator/zip_iterator.h>
- #include <thrust/tuple.h>
- // 1、stl的容器都默认会有迭代器的 通用的
- // 2、不同容器的迭代器可以组合成一个元组 thrust::tuple
- // 3、迭代器元组可以被封装成zip_iterator thrust::zip_iterator
- // 4、zip_iterator的初始化 thrust::make_tuple
- // 5、最终我们使用的就是一个zip_iterator的迭代器对象,像使用stl中的普通迭代器一样
- // 6、通过zip_iterator取值 thrust::get<1>(zip_iter[2])
- int main(){
- //使用zip_iterator迭代器、make_tuple、thrust::get<0>
- thrust::device_vector<int> int_v(3);
- int_v[0] = 0; int_v[1] = 1; int_v[2] = 2;
- thrust::device_vector<float> float_v(3);
- float_v[0] = 0.0f; float_v[1] = 1.0f; float_v[2] = 2.0f;
- thrust::device_vector<char> char_v(3);
- char_v[0] = 'a'; char_v[1] = 'b'; char_v[2] = 'c';
- // typedef these iterators for shorthand stl的迭代器
- typedef thrust::device_vector<int>::iterator IntIterator;
- typedef thrust::device_vector<float>::iterator FloatIterator;
- typedef thrust::device_vector<char>::iterator CharIterator;
- // typedef a tuple of these iterators 定义一个 迭代器元组
- typedef thrust::tuple<IntIterator, FloatIterator, CharIterator> IteratorTuple;
- // typedef the zip_iterator of this tuple 迭代器元组外又封装了一层 zip迭代器
- typedef thrust::zip_iterator<IteratorTuple> ZipIterator;
- // finally, create the zip_iterator
- ZipIterator zip_iter(thrust::make_tuple(int_v.begin(), float_v.begin(), char_v.begin()));
- std::cout << "开始使用zip迭代器对象了!" << std::endl;
- // zip_iter[2] = (2, 2.0f, 'c')
- std::cout << thrust::get<0>(zip_iter[2]) << std::endl;
- std::cout << thrust::get<1>(zip_iter[2]) << std::endl;
- std::cout << thrust::get<2>(zip_iter[2]) << std::endl;
- //std::cout << thrust::get<2>(zip_iter[3]) << std::endl; 越界错误
- // *zip_iter; // returns (0, 0.0f, 'a')
- // zip_iter[0]; // returns (0, 0.0f, 'a')
- // zip_iter[1]; // returns (1, 1.0f, 'b')
- // zip_iter[2]; // returns (2, 2.0f, 'c')
- // thrust::get<0>(zip_iter[2]); // returns 2
- // thrust::get<1>(zip_iter[0]); // returns 0.0f
- // thrust::get<2>(zip_iter[1]); // returns 'b'
- // zip_iter[3] is an out-of-bounds error
-
- return 0;
- }
复制代码
|
|