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>的更多相关文章

  1. 浅谈JSP中include指令与include动作标识的区别

    JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...

  2. Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include

    问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...

  3. error RC1015: cannot open include file 'afxres.h' 解决办法

    在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...

  4. Mybatis常用总结:参数,返回,执行sql,include等

    1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...

  5. jsp中的@include与jsp:include区别详解

    1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...

  6. JSP中编译指令include与动作指令include的区别

    include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...

  7. C/C++ 中的include

    当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...

  8. 织梦多语言站点,{dede:include filename=''/}引入问题

    织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...

  9. PHP 站点相对包含,路径的问题解决方法(include,require)

    以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...

  10. 如何让include标签包裹的布局置于屏幕最下方?

    如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性  a ...

随机推荐

  1. Python之路第五天,基础(6)-模块

    模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个 ...

  2. 求解printf函数?

    求大神解释一下下面的代码为什么答案不是1 2,而是1 0. #include <stdio.h> int ans = 0; int a() { ans = 1; return ans++; ...

  3. windows开机启动nginx

    1 .http://www.cuplayer.com/player/PlayerCode/Nginx/2014/0919/1577.html 2. http://www.cnblogs.com/xus ...

  4. 调magento自定义模板发邮件

    1. 设置邮件模板 <global> <template> <email> <custom_email_template1 module="Samp ...

  5. 固定cell.imageView.image的大小

    cell.imageView.image的大小 会随着Cell的高度而变化,不同的图片显示的也不一样,在网上找了几种方法,简单方便的是下面这种: UIImage *icon = [UIImage im ...

  6. 一个tabBarController管理多个Storyboard

    随着项目的业务逻辑越来越复杂,随着项目越来越大,那么我们Storybard中得控制器就越来越多, 就越来越难以维护.然而使用Storyborad又能更方便的帮助我们做屏幕适配(PS:尤其在6.6+出来 ...

  7. 使用Abator生产ibatis配置文件

    什么都不说了,直接进入正题. 插件安装地址:http://ibatis.apache.org/tools/abator 里面有name和url,填了就可以安装了. 通过菜单的 File > Ne ...

  8. 清空DateTimePicker控件的好方法

    [控件ID,不要加这个方括号].Format = DateTimePickerFormat.Custom; [控件ID,不要加这个方括号].CustomFormat = " "; ...

  9. php 实现二进制加法运算

    php实现二进制加法: 思路:没有工作中应用过此场景,但十进制的加法还是经常做的,能不能用十进制加法变相实现呢? 答案是可以的,并且php也提供进制间转换的函数,我的实现使用了 bindec():二进 ...

  10. asp.net ImageMap控件

    ImageMap 控件可创建包含定义的作用点区域的图像.当用户单击作用点区域时,该控件可生成到服务器的回发或导航到指定的 URL 首先是添加一个asp:ImageMap 选择asp:CircleHot ...