9.variant move function change_cast

  • 包含的头文件
     1 #include <iostream>
     2 #include <string>
     3 #include <boost/array.hpp>
     4 //异构的容器
     5 #include <boost/any.hpp>
     6 #include <vector>
     7 #include <typeinfo>
     8 #include <algorithm>
     9 #include <boost/bind.hpp>
    10 #include <functional>
    11 #include <boost/variant.hpp>
    12 #include <boost/move/move.hpp>
    13 #include <boost/function.hpp>
    14 #include <boost/bind.hpp>
    15 #include <boost/lexical_cast.hpp>
    16 #include <boost/numeric/conversion/cast.hpp>
    17 #include <boost/cast.hpp>
    18 using namespace std;
    19 using namespace boost;

     

  • 函数包装器,包装仿函数,以及一个类包装另外一个类的函数
      1 int add(int a, int b)
      2 {
      3     cout << a + b << endl;
      4     return a + b;
      5 }
      6 
      7 //常规包装器
      8 void main1()
      9 {
     10     vector<int> myint = { 1,2,3,4,5 };
     11     //绑定操作
     12     //for_each(myint.begin(), myint.end(), bind(add, 10, _1));
     13 
     14     //绑定lambda表达式,借助函数包装器
     15     boost::function<void(int,int)> fun = [](int a, int b)
     16     {
     17         cout << a + b << endl;
     18     };
     19     for_each(myint.begin(), myint.end(), bind(fun,10,_1));
     20     cin.get();
     21 }
     22 
     23 //继承异构二元操作类
     24 class addit :public std::binary_function<int, int, void>
     25 {
     26 public:
     27     void operator()(int i, int j)
     28     {
     29         cout << i + j << endl;
     30     }
     31 };
     32 
     33 //绑定仿函数
     34 void main2()
     35 {
     36     vector<int> myint = { 1,2,3,4,5 };
     37     //绑定仿函数
     38     for_each(myint.begin(), myint.end(), bind(addit(), 10, _1));
     39     cin.get();
     40 }
     41 
     42 void main3()
     43 {
     44     //myv可以是四种类型之一的任何一种类型
     45     typedef boost::variant<int, double, char, const char *> myv;
     46     std::vector<myv> s_values;
     47     s_values.push_back(10);
     48     s_values.push_back('X');
     49     s_values.push_back(10.9);
     50     s_values.push_back("hello");
     51     s_values.push_back('z');
     52     //根据数据类型来获取
     53     char ch = boost::get<char>(s_values.back());
     54     double db = boost::get<double>(s_values.at(2));
     55     cout << db << endl;
     56 
     57     cin.get();
     58 }
     59 
     60 //manager管理worker 类与类之间通信
     61 class manager
     62 {
     63 public:
     64     //函数指针
     65     boost::function<void(int)> workid;
     66     void setcallback(boost::function<void(int)> newworkid)
     67     {
     68         workid = newworkid;
     69     }
     70 
     71     void allgo()
     72     {
     73         for (int i = 0; i < 10; i++)
     74         {
     75             if (workid)
     76             {
     77                 workid(i);
     78             }
     79         }
     80     }
     81 };
     82 
     83 class worker
     84 {
     85 public:
     86     int id;
     87     void run(int toid)
     88     {
     89         id = toid;
     90         cout << id << "干活" << endl;
     91     }
     92 };
     93 
     94 void main6()
     95 {
     96     manager m;
     97     worker w;
     98     //传递一个绑定的函数,调用者是w,需要一个参数
     99     m.setcallback(boost::bind(&worker::run, &w, _1));
    100     m.allgo();
    101     cin.get();
    102 }
     1 //function
     2 void main5()
     3 {
     4     boost::function<int(char *)>fun = atoi;
     5     cout << fun("123") + fun("34") << endl;
     6 
     7     fun = strlen;
     8     cout << fun("234") << endl;
     9     cin.get();
    10 }

     

  • 左值转化为右值
     1 //右值引用
     2 void show(int &&data)
     3 {
     4     cout << data << endl;
     5 }
     6 
     7 void main4()
     8 {
     9     int a = 4;
    10     //左值转化为右值
    11     show(std::move(a));
    12     show(boost::move(a));
    13     cin.get();
    14 }

     

  • 类型转换与类类型之间的转换,失败会异常
     1 //类型转换
     2 void main7()
     3 {
     4     int i = boost::lexical_cast<int>("849");
     5     cout << i << endl;//转换失败则显示异常
     6 
     7     char str[5] = { '1','2','3','4','5' };
     8     i = boost::lexical_cast<int>(str, 5);
     9     cout << i << endl;
    10 
    11     cin.get();
    12 }
    13 
    14 //转换成字符串类型
    15 void main8()
    16 {
    17     std::string str = boost::lexical_cast<std::string>("12345");
    18     cin.get();
    19 }
    20 
    21 void main9()
    22 {
    23     std::string str = boost::lexical_cast<std::string>("12345");
    24     //转换安全(转换失败会报异常)
    25     int num = boost::numeric_cast<int>("1234");
    26     cin.get();
    27 }
    28 
    29 class A
    30 {
    31 
    32 };
    33 
    34 class B :public A
    35 {
    36 
    37 };
    38 
    39 //类类型之间的转换
    40 void main()
    41 {
    42     B bobj;
    43     //转换失败会报异常
    44     boost::polymorphic_cast<A*>(&bobj);
    45     cin.get();
    46 }

     

posted @ 2018-03-26 21:22  喵小喵~  阅读(178)  评论(0编辑  收藏  举报