|
// 1、使用make_zip_iterator、make_tuple同时操作多个stl的迭代器
// 2、泛型里面又是泛型:这种场景
// 3、泛型定义的时候应先满足 大类型,大类型里面的小类型用typedef即可
- #include <thrust/device_vector.h>
- #include <iostream>
- #include <thrust/iterator/zip_iterator.h>
- #include <thrust/tuple.h>
- #include <string>
- // 1、使用make_zip_iterator、make_tuple同时操作多个stl的迭代器
- // 2、泛型里面又是泛型:这种场景
- // 3、泛型定义的时候应先满足 大类型,大类型里面的小类型用typedef即可
- // template <typename T>
- // void print_vector2(thrust::device_vector<T> &v){
- // // 这样写 为什么不对?
- // thrust::device_vector<T>::iterator it;
- // for(it, it!=v.end();it++){
- // std::cout << *it << std::endl;
- // }
- // }
- // template <typename T>
- // void print_vector(thrust::device_vector<T> &v){
- // thrust::device_vector<T>::iterator it;
- // for(it, it!=v.end();it++){
- // std::cout << *it << std::endl;
- // }
- // }
- template <typename Vector>
- void print(const std::string& s, const Vector& v)
- {
- //v这个类型为:thrust::device_vector<int> 或 thrust::device_vector<float>
- //本身就是泛型啊 泛型里面又是泛型:这种场景
- typedef typename Vector::value_type T; //这个操作比较关键? T才相当于int 或 float
- // 泛型定义的时候应先满足 大类型,大类型里面的小类型用typedef即可
- std::cout << s;
- thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
- std::cout << std::endl;
- }
- int main(){
- thrust::device_vector<int> int_in(3), int_out(3);
- int_in[0] = 0;
- int_in[1] = 1;
- int_in[2] = 2;
- thrust::device_vector<float> float_in(3), float_out(3);
- float_in[0] = 0.0f;
- float_in[1] = 10.0f;
- float_in[2] = 20.0f;
- // copy (first, last, new_first)
- // 使用make_zip_iterator、make_tuple同时操作多个stl的迭代器
- thrust::copy(thrust::make_zip_iterator(thrust::make_tuple(int_in.begin(), float_in.begin())),
- thrust::make_zip_iterator(thrust::make_tuple(int_in.end(), float_in.end())),
- thrust::make_zip_iterator(thrust::make_tuple(int_out.begin(),float_out.begin())));
- // int_out is now [0, 1, 2]
- //print_vector<int>(int_out);
- print<thrust::device_vector<int>>("看int_out_jjj=", int_out);
- print("看int_out(调模板函数,简写,不合理啊)=", int_out); //为啥不用<>啊
- // float_out is now [0.0f, 10.0f, 20.0f]
- print<thrust::device_vector<float>>("看float_out=", float_out);
- //print_vector<float>(float_out);
-
- return 0;
- }
复制代码
|
|