9.variant move function change_cast
- 包含的头文件
#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的更多相关文章
- Character Controller (角色控制器) 中 Move()和SimpleMove() 的区别
首先给出两者的圣典: CollisionFlagsMove(Vector3motion); Description A more complex move function taking absolu ...
- move.js 源码 学习笔记
源码笔记: /* move.js * @author:flfwzgl https://github.com/flfwzgl * @copyright: MIT license * Sorrow.X - ...
- move.js运动插件
move.js 运动插件是一款针对元素动画效果的插件.可以运用此插件制作出各类元素效果. 插件GitHub地址:https://github.com/visionmedia/move.js 下面整理学 ...
- coffeescript 1.8.0 documents
CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque ...
- Add GUI to connect to SQL
(*********************************************************************************) (* *) (* Below i ...
- 分享ES6中比较常用又强大的新特性
前言 es6有很多新东西,但是感觉常用的并不是很多,这里学习记录了一些我自己认为非常常用又强大的新特性. scoping 实用的块级作用域,let x = xxx 可以声明一个块级作用域的局部变量,简 ...
- Delphi的程序单元、结构、基础知识(转)
Object Passal的程序结构很特殊,与其它语言如C++,Object Windows等结构都不同.一个Delphi程序由多个称为单元的源代码模块组成.使用单元可以把一个大型程序分成多个逻辑相关 ...
- how to use coffee script
TABLE OF CONTENTS TRY COFFEESCRIPT ANNOTATED SOURCE CoffeeScript is a little language that compiles ...
- wxpython wx.windows的API
wx.Window is the base class for all windows and represents any visible object on screen. All control ...
随机推荐
- kentico中提示Message: An invalid SQL query was used.
在调用CMSAbstractWebPart类的GetValue方法的时候出错. namespace CMS.PortalEngine.Web.UI{ /// <summary> /// B ...
- scrollView中内部控件的悬停
以下图为例,图片,红色view和蓝色view是添加在scrollView上的,向上拖动,红色view停留在屏幕顶端不动,其它的继续滚动,向下拖动后,红色view跟着下来 代码如下:(注意的是scrol ...
- 类扩展和category的小区别
类扩展可以给类声明新的变量(属性),但是方法的实现只能在.m中实现 category可以给类声明新的方法实例,但是不可以添加变量(属性)
- Bootstrap表格内容居中
1.<th style='text-align: center;'>host</th> 水平居中 2.<td rowspan=$row_host1 style='vert ...
- 转:Redis介绍及常用命令大全
一 Redis介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发 ...
- Asp.Net Core部署到Linux服务器
从2016年7月, .NET Core1.0 正式发布开始,由于时间问题,我没怎么关注过.NET Core,最近刚抽出点时间研究了下,先讲下如何把ASP.NET Core部署到Linux上吧.这里我用 ...
- APUE学习笔记6——线程和线程同步
1 概念 线程是程序执行流的最小单元.线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的 ...
- 为什么叫Unity3d为脚本语言
初接触Unity,看到大家说的都是工作主要是写脚本语言. 一直纳闷为什么说脚本语言呢,c#可不是脚本语言啊. -- -- 后来释然,说它是脚本语言是因为传统程序都是由代码构成的(像iOS.Androi ...
- 全文检索lucene6.1的检索方式
背景: 工作任务完成后,闲暇之计给自己充充电! Lucene是一个纯java全文检索工具包,采用倒排索引原理. 全文检索:指的是计算机索引程序通过扫描文章的每一个词,对每一个词建立一个索引,并指明该词 ...
- TCP/IP 三次握手和HTTP过程
0 引言 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在“无差别”的网络之上. 1 T ...