• 包含的头文件

     #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();
    }

9.variant move function change_cast的更多相关文章

  1. Character Controller (角色控制器) 中 Move()和SimpleMove() 的区别

    首先给出两者的圣典: CollisionFlagsMove(Vector3motion); Description A more complex move function taking absolu ...

  2. move.js 源码 学习笔记

    源码笔记: /* move.js * @author:flfwzgl https://github.com/flfwzgl * @copyright: MIT license * Sorrow.X - ...

  3. move.js运动插件

    move.js 运动插件是一款针对元素动画效果的插件.可以运用此插件制作出各类元素效果. 插件GitHub地址:https://github.com/visionmedia/move.js 下面整理学 ...

  4. coffeescript 1.8.0 documents

    CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque ...

  5. Add GUI to connect to SQL

    (*********************************************************************************) (* *) (* Below i ...

  6. 分享ES6中比较常用又强大的新特性

    前言 es6有很多新东西,但是感觉常用的并不是很多,这里学习记录了一些我自己认为非常常用又强大的新特性. scoping 实用的块级作用域,let x = xxx 可以声明一个块级作用域的局部变量,简 ...

  7. Delphi的程序单元、结构、基础知识(转)

    Object Passal的程序结构很特殊,与其它语言如C++,Object Windows等结构都不同.一个Delphi程序由多个称为单元的源代码模块组成.使用单元可以把一个大型程序分成多个逻辑相关 ...

  8. how to use coffee script

    TABLE OF CONTENTS TRY COFFEESCRIPT ANNOTATED SOURCE CoffeeScript is a little language that compiles ...

  9. wxpython wx.windows的API

    wx.Window is the base class for all windows and represents any visible object on screen. All control ...

随机推荐

  1. 英语发音规则---H字母

    英语发音规则---H字母 一.总结 一句话总结: 1.H发[h]音? hot [hɒt] adj. 热的 house [haʊs] n. 住宅 head [hed] n. 头:头痛 hat [hæt] ...

  2. Thread.setDaemon设置说明

    转载地址:http://blog.csdn.net/m13666368773/article/details/7245570 Thread.setDaemon的用法,经过学习以后了解: 1. setD ...

  3. AMD cpu 下 Pytorch 多卡并行卡死问题解决

    dataparallel not working on nvidia gpus and amd cpus   https://github.com/pytorch/pytorch/issues/130 ...

  4. Python笔记(四)

    # -*- coding:utf-8 -*- # 控制语句 # if...else... print "********************1********************** ...

  5. C# 正则表达式

    C# 正则表达式 正则表达式 是一种匹配输入文本的模式..Net 框架提供了允许这种匹配的正则表达式引擎.模式由一个或多个字符.运算符和结构组成. 定义正则表达式 下面列出了用于定义正则表达式的各种类 ...

  6. js 数据类型判断

    判断type类型 isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } ...

  7. javascript中封装获取样式属性值的兼容方法

    function getStyle(obj, attr) { if (window.getComputedStyle) { return window.getComputedStyle(obj, nu ...

  8. struts.xml里面子元素的配置

    <struts> <!-- package:包,struts2使用package来组织模块 name属性:必须,用于其他的包引用当前包 extends:当前包继承哪个包,继承的,即可 ...

  9. BootStrap复习总结

    Bootstrap3 复习总结: 1. 栅格系统: sm:768px md:992px lg:1200px (响应式布局) 1.横向分割页面的空间. 2.在分割的同时考虑到响应式. 内部实现是12列的 ...

  10. C语言基本语法——字符串

    1.什么是字符串 2.字符串与普通字符数组的区别 3.字符串的定义方式 4.字符串的使用 5.什么是字符串数组 6.字符串数组的赋值 7.字符串数组的遍历 1.什么是字符串 • 用双引号引起来的多个字 ...