#include <functional>
1 bind(引用内部函数, 实体对象的地址, 占位符);
2 bind1st
3 function
1 auto 变量名 = bind(引用内部函数, 实体对象的地址, 占位符);
#include <iostream>
#include <functional>
using namespace std; //仿函数,创建一个函数指针,引用一个结构体内部或者一个类内部的public公有函数 struct MyStruct
{
void add1(int a)
{
std::cout << a << std::endl;
} void add2(int a, int b)
{
std::cout << a + b << std::endl;
} void add3(int a, int b, int c)
{
std::cout << a + b + c << std::endl;
}
}; void main()
{
MyStruct struct1;//创建一个结构体变量 //auto自动变量,地址,函数指针
//对于参数要使用占位符 std::placeholders::_1
//auto 变量名 = bind(引用内部函数, 实体对象的地址, 占位符);
auto func = bind(&MyStruct::add1, &struct1, std::placeholders::_1); auto func2 = bind(&MyStruct::add2, &struct1, std::placeholders::_1, std::placeholders::_2); auto func3 = bind(&MyStruct::add3, &struct1, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); func();// func2(, );// func3(, , );//60 //创建一个函数指针,指向结构体内部的函数。由于结构体的数据是独有的,而函数是共享的,因此无法指向某个结构体变量的函数,只能指向结构体的函数
//函数通过调用,调用需要传递对象名
void(MyStruct::*p)(int) = &MyStruct::add1;//使用函数也可以解决,但是没有bind好用 system("pause");
}
2 bind1st
std::bind1st(std::greater<int>(), 3)
//bind1st绑定一个函数,greater也是函数,比大小
//作用:从头到尾,查找比3小的第一个元素
//用途,查找不及格的人
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional> int main()
{
std::vector<int>myvector; myvector.push_back();
myvector.push_back();
myvector.push_back();
myvector.push_back();
myvector.push_back(); auto ib = myvector.begin();
auto ie = myvector.end(); for (; ib != ie; ib++)
{
std::cout << *ib << std::endl;
}
std::cout << std::endl; auto ifind = find_if(myvector.begin(), myvector.end(), std::bind1st(std::greater<int>(), ));//bind1st绑定一个函数,greater也是函数,比大小
//作用:从头到尾,查找比3小的第一个元素
//用途,查找不及格的人 std::cout << *ifind << std::endl; return ;
}
std::bind1st(std::greater<int>(), 3)
另外写一个函数实现
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional> bool less3(int x)
{
return x < ;
} int main()
{
std::vector<int>myvector; myvector.push_back();
myvector.push_back();
myvector.push_back();
myvector.push_back();
myvector.push_back(); auto ib = myvector.begin();
auto ie = myvector.end(); for (; ib != ie; ib++)
{
std::cout << *ib << std::endl;
}
std::cout << std::endl; auto ifind = find_if(myvector.begin(), myvector.end(), less3);//bind1st绑定一个函数,greater也是函数,比大小
//作用:从头到尾,查找比3小的第一个元素
//用途,查找不及格的人 std::cout << *ifind << std::endl; return ;
}
3 std::function
std::function实现函数包装器
std::function实现函数包装器
//第一,设计执行接口,接口可以设计关卡(收费,插入if...else)、计数器
//第二,函数包装器依赖于函数模板,实现通用泛型
//第三,函数代码可以内嵌在另外一个函数,实现函数怀孕
//第四,函数包装器可以用于管理内嵌函数和外部函数调用
函数包装器管理内嵌函数
#include <iostream>
#include <functional>
using namespace std; //函数包装器
//第一,设计执行接口,接口可以设计关卡(收费,插入if...else)、计数器
//第二,函数包装器依赖于函数模板,实现通用泛型
//第三,函数代码可以内嵌在另外一个函数,实现函数怀孕
//第四,函数包装器可以用于管理内嵌函数和外部函数调用 //函数包装器,T是数据类型,F是函数
template <typename T, typename F>
T run(T v, F f)//第一个参数是数据,第二个参数是函数
{
static int count = ;//计数器
count++;
std::cout << "一个参数的包装器 执行" << count << "次" << std::endl;
if (count > )//通过计数器,限定函数执行次数
{
T vx();
return vx;
} return f(v);//函数传入参数
} template <typename T, typename F>
T run(T v1, T v2, F f)//第一个参数是数据,第二个参数是数据,第三个参数是函数
{
return f(v1, v2);//函数传入参数
} void main()
{
double db = 12.9;
int num1 = ;
int num2 = ; // <返回值类型(参数类型)>
//fun1是函数指针
std::function <double(double)>fun1 = [](double u)
{
return u * ;
}; std::function <double(double)>fun2 = [](double u)
{
return u*u;
}; // <返回值类型(参数类型, 参数类型)>
std::function <int(int, int)>fun3 = [](int u1, int u2)
{
return u1 + u2;
}; std::cout << run(db, fun1) << std::endl;//25.8 std::cout << run(db, fun2) << std::endl; std::cout << run(num1, num2, fun3) << std::endl;// system("pause");
}
函数包装器管理外部函数
#include <iostream>
#include <functional>
using namespace std; //函数包装器
//第一,设计执行接口,接口可以设计关卡(收费,插入if...else)、计数器
//第二,函数包装器依赖于函数模板,实现通用泛型
//第三,函数代码可以内嵌在另外一个函数,实现函数怀孕
//第四,函数包装器可以用于管理内嵌函数和外部函数调用 //函数包装器,T是数据类型,F是函数
template <typename T, typename F>
T run(T v1, T v2, F f)//第一个参数是数据,第二个参数是数据,第三个参数是函数
{
return f(v1, v2);//函数传入参数
} int cheng(int a, int b)//外部函数,实现乘法
{
return a*b;
} void main()
{
double db = 12.9;
int num1 = ;
int num2 = ; //fun4是函数指针
// <函数返回值类型(参数类型, 参数类型)>
std::function <int(int, int)>fun4 = cheng; std::cout << run(num1, num2, fun4) << std::endl;// system("pause");
}
#include <functional>的更多相关文章
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include
问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...
- error RC1015: cannot open include file 'afxres.h' 解决办法
在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...
- Mybatis常用总结:参数,返回,执行sql,include等
1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...
- jsp中的@include与jsp:include区别详解
1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...
- JSP中编译指令include与动作指令include的区别
include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...
- C/C++ 中的include
当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...
- 织梦多语言站点,{dede:include filename=''/}引入问题
织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...
- PHP 站点相对包含,路径的问题解决方法(include,require)
以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...
- 如何让include标签包裹的布局置于屏幕最下方?
如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性 a ...
随机推荐
- 通过读取excel数据和mysql数据库数据做对比(一)-win环境准备
要想操作excel和mysql首先需要安装python,然后是安装excel和mysql插件: 第一步安装python: 直接百度搜索,下载安装就可以了. 第二步安装excel插件: 首先到这个htt ...
- popen()函数详解
popen()函数 /*============================================ > Copyright (C) 2014 All rights reserved ...
- raspberrypi VNC server
安装apt-get install tightvncserver tightvnc-java 启动vncserver -name vnc_raspi -depth 24 -geometry 800x6 ...
- ID卡常见型号
EM ID卡,主要是采用瑞士EM或台湾GK公司的4100.4102系列IC芯片 + 线圈 + 卡基封装而成. (1)4001感应式ID厚卡:台湾4001 COB 特征:普通型感应卡,厚薄适中,带有ID ...
- 关于IE的兼容模式
前言 为了帮助确保你的网页在所有未来的IE版本都有一致的外观,IE8引入了文件兼容性.在IE6中引入一个增设的兼容性模式,文件兼容性使你能够在IE呈现你的网页时选择特定编译模式. 新的IE为了确保网页 ...
- Linux学习笔记4-三种不同类型的软件的安装(绿色软件、rpm软件、源代码软件)
在Linux下软件分三种: 1.绿色软件:即不用安装直接就能用的软件 2.rpm安装包:以rpm结尾的可执行文件 3.源码文件:没有进行过编译和打包的文件,需要编译后再进行安装 一.绿色软件的安装 ...
- 精通CSS+DIV基础总结(一)
这段时间学习了玩了DIV+CSS的视频,感觉效率不高.前边的Javascript总结的不好,但是看了后边的JQuery,觉得学习的再多一点,再进行Javascript的总结.DIV+CSS总结,估计会 ...
- Linux mint 17中文输入法安装,改动linux mint与windows7双系统启动顺序
安装好linux mint17后,进入mint系统,首先须要一个比較合适的中文输入法. 一.首先迎来的就是安装中文输入法了,之前听说搜狗为ubuntu kinly定制了输入法,所以就想安装搜狗输入法, ...
- 读取系统执行状态的shell脚本
近期在学习shell.老大让写一个读取系统配置信息的脚本当作练习和工作验收,我就写了这么一个脚本,读取操作系统,内核,网卡,cpu,内存,磁盘等信息,目的是让看的人一眼就能看出这台机子的配置以及眼下的 ...
- Setup Factory
Setup Factory 生成安装程序