• 包含的头文件

     #include <iostream>
    #include <string>
    #include <boost/array.hpp>
    //异构的容器
    #include <boost/any.hpp>
    #include <vector>
    #include <typeinfo>
    #include <algorithm>
    #include <boost/bind.hpp>
    #include <functional>
    #include <boost/variant.hpp>
    #include <boost/move/move.hpp>
    #include <boost/function.hpp>
    #include <boost/bind.hpp>
    #include <boost/lexical_cast.hpp>
    #include <boost/numeric/conversion/cast.hpp>
    #include <boost/cast.hpp>
    using namespace std;
    using namespace boost;
  • 函数包装器,包装仿函数,以及一个类包装另外一个类的函数
     int add(int a, int b)
    {
    cout << a + b << endl;
    return a + b;
    } //常规包装器
    void main1()
    {
    vector<int> myint = { ,,,, };
    //绑定操作
    //for_each(myint.begin(), myint.end(), bind(add, 10, _1)); //绑定lambda表达式,借助函数包装器
    boost::function<void(int,int)> fun = [](int a, int b)
    {
    cout << a + b << endl;
    };
    for_each(myint.begin(), myint.end(), bind(fun,,_1));
    cin.get();
    } //继承异构二元操作类
    class addit :public std::binary_function<int, int, void>
    {
    public:
    void operator()(int i, int j)
    {
    cout << i + j << endl;
    }
    }; //绑定仿函数
    void main2()
    {
    vector<int> myint = { ,,,, };
    //绑定仿函数
    for_each(myint.begin(), myint.end(), bind(addit(), , _1));
    cin.get();
    } void main3()
    {
    //myv可以是四种类型之一的任何一种类型
    typedef boost::variant<int, double, char, const char *> myv;
    std::vector<myv> s_values;
    s_values.push_back();
    s_values.push_back('X');
    s_values.push_back(10.9);
    s_values.push_back("hello");
    s_values.push_back('z');
    //根据数据类型来获取
    char ch = boost::get<char>(s_values.back());
    double db = boost::get<double>(s_values.at());
    cout << db << endl; cin.get();
    } //manager管理worker 类与类之间通信
    class manager
    {
    public:
    //函数指针
    boost::function<void(int)> workid;
    void setcallback(boost::function<void(int)> newworkid)
    {
    workid = newworkid;
    } void allgo()
    {
    for (int i = ; i < ; i++)
    {
    if (workid)
    {
    workid(i);
    }
    }
    }
    }; class worker
    {
    public:
    int id;
    void run(int toid)
    {
    id = toid;
    cout << id << "干活" << endl;
    }
    }; void main6()
    {
    manager m;
    worker w;
    //传递一个绑定的函数,调用者是w,需要一个参数
    m.setcallback(boost::bind(&worker::run, &w, _1));
    m.allgo();
    cin.get();
    }
     //function
    void main5()
    {
    boost::function<int(char *)>fun = atoi;
    cout << fun("") + fun("") << endl; fun = strlen;
    cout << fun("") << endl;
    cin.get();
    }
  • 左值转化为右值
     //右值引用
    void show(int &&data)
    {
    cout << data << endl;
    } void main4()
    {
    int a = ;
    //左值转化为右值
    show(std::move(a));
    show(boost::move(a));
    cin.get();
    }
  • 类型转换与类类型之间的转换,失败会异常
     //类型转换
    void main7()
    {
    int i = boost::lexical_cast<int>("");
    cout << i << endl;//转换失败则显示异常 char str[] = { '','','','','' };
    i = boost::lexical_cast<int>(str, );
    cout << i << endl; cin.get();
    } //转换成字符串类型
    void main8()
    {
    std::string str = boost::lexical_cast<std::string>("");
    cin.get();
    } void main9()
    {
    std::string str = boost::lexical_cast<std::string>("");
    //转换安全(转换失败会报异常)
    int num = boost::numeric_cast<int>("");
    cin.get();
    } class A
    { }; class B :public A
    { }; //类类型之间的转换
    void main()
    {
    B bobj;
    //转换失败会报异常
    boost::polymorphic_cast<A*>(&bobj);
    cin.get();
    }

最新文章

  1. Java核心技术点之多线程
  2. bzoj1415[NOI2005]聪聪和可可-期望的线性性
  3. Func&lt;T&gt;与Action&lt;T&gt;委托泛型介绍:转
  4. stm32学习笔记——外部中断的使用
  5. listview 滑动以后设置最上面一行为整行展示
  6. 【C++面试】常考题复习
  7. js获取项目根路径
  8. git克隆远程项目分支到本地对应分支
  9. PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: &#39;C:\\Users\\video\\AppData\\Local\\Temp\\tmpfipzk8ma&#39;--问题解决
  10. interview collect
  11. Nginx 之六: Nginx十万并发优化
  12. Objective-C对象模型及应用
  13. 64位win7系统下面安装pyOpenSSL
  14. iOS常用控件尺寸大集合
  15. tuxedo 提供buildserver命令编译服务器进程
  16. k8s对接ceph存储
  17. kubernets基础
  18. 周强 201771010141 《面向对象程序设计(java)》第七周学习总结
  19. 无法加载ISAPI 筛选器 当前配置只支持加载为 AMD64 处理器体系结构创建的映像
  20. C++11 使用 std::async创建异步程序

热门文章

  1. SpringBoot项目部署
  2. BroadcastReceiver register 广播的动态注册方式
  3. FrameLsyout
  4. Win10 build package error collections
  5. php基础-----常量const和define的区别
  6. 怎样学CSS?
  7. CF950B Intercepted Message_双指针法
  8. 最近邻插值法&amp;线性插值&amp;双线性插值&amp;三线性插值
  9. C# 基础复习 二 面向对象
  10. node——简单的服务器启动+乱码问题解决,响应报文头