东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 2453|回复: 0
打印 上一主题 下一主题

[C/C++] 使用make_zip_iterator、make_tuple同时操作多个stl的迭代器

[复制链接]

1366

主题

1857

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14458
QQ
跳转到指定楼层
楼主
发表于 2021-7-16 11:25:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式



// 1、使用make_zip_iterator、make_tuple同时操作多个stl的迭代器
// 2、泛型里面又是泛型:这种场景
// 3、泛型定义的时候应先满足 大类型,大类型里面的小类型用typedef即可





  1. #include <thrust/device_vector.h>
  2. #include <iostream>
  3. #include <thrust/iterator/zip_iterator.h>
  4. #include <thrust/tuple.h>
  5. #include <string>

  6. // 1、使用make_zip_iterator、make_tuple同时操作多个stl的迭代器
  7. // 2、泛型里面又是泛型:这种场景
  8. // 3、泛型定义的时候应先满足 大类型,大类型里面的小类型用typedef即可


  9. // template <typename T>
  10. // void print_vector2(thrust::device_vector<T> &v){
  11. //     // 这样写 为什么不对?
  12. //     thrust::device_vector<T>::iterator it;
  13. //     for(it, it!=v.end();it++){
  14. //         std::cout << *it << std::endl;
  15. //     }
  16. // }


  17. // template <typename T>
  18. // void print_vector(thrust::device_vector<T> &v){
  19. //     thrust::device_vector<T>::iterator it;
  20. //     for(it, it!=v.end();it++){
  21. //         std::cout << *it << std::endl;
  22. //     }
  23. // }


  24. template <typename Vector>
  25. void print(const std::string& s, const Vector& v)
  26. {
  27.   //v这个类型为:thrust::device_vector<int> 或 thrust::device_vector<float>
  28.   //本身就是泛型啊  泛型里面又是泛型:这种场景
  29.   typedef typename Vector::value_type T;  //这个操作比较关键?  T才相当于int 或 float
  30.   // 泛型定义的时候应先满足 大类型,大类型里面的小类型用typedef即可

  31.   std::cout << s;
  32.   thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
  33.   std::cout << std::endl;
  34. }


  35. int main(){
  36.     thrust::device_vector<int> int_in(3), int_out(3);
  37.     int_in[0] = 0;
  38.     int_in[1] = 1;
  39.     int_in[2] = 2;
  40.     thrust::device_vector<float> float_in(3), float_out(3);
  41.     float_in[0] =  0.0f;
  42.     float_in[1] = 10.0f;
  43.     float_in[2] = 20.0f;
  44.     // copy (first, last, new_first)
  45.     // 使用make_zip_iterator、make_tuple同时操作多个stl的迭代器
  46.     thrust::copy(thrust::make_zip_iterator(thrust::make_tuple(int_in.begin(), float_in.begin())),
  47.                  thrust::make_zip_iterator(thrust::make_tuple(int_in.end(),   float_in.end())),
  48.                  thrust::make_zip_iterator(thrust::make_tuple(int_out.begin(),float_out.begin())));
  49.     // int_out is now [0, 1, 2]
  50.     //print_vector<int>(int_out);
  51.     print<thrust::device_vector<int>>("看int_out_jjj=", int_out);
  52.     print("看int_out(调模板函数,简写,不合理啊)=", int_out);   //为啥不用<>啊
  53.     // float_out is now [0.0f, 10.0f, 20.0f]
  54.     print<thrust::device_vector<float>>("看float_out=", float_out);
  55.     //print_vector<float>(float_out);
  56.    

  57.     return 0;
  58. }




复制代码


让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-6-23 11:41 , Processed in 0.209890 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表