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. ubuntu下的notepad++

    安装方法: 终端输入命令:sudo apt-get install scite 安装完成后dash中输入scite查找已经安装的scite,拖动到桌面快捷方式.

  2. MYSQL中limit的使用

    limit是mysql的语法select * from table limit m,n其中m是指记录开始的索引,从0开始,表示第一条记录n是指从第m+1条开始,取n条.select * from ta ...

  3. Developer‘s提升开发效率的工具和插件或编程语言

    1.Git 之前也有过不少版本控制的工具.有好的,也有糟糕的.不过它们都或多或少地误入歧途了. 这时候Git出现了.一旦你用上了这个神奇的工具,很难相像你还会碰到比它更好的了. 还没用过Git?试一下 ...

  4. html和js

    1.<input type="button" value="Hello world!"> 2.<button type="butto ...

  5. Struts2 请求参数接收

    在Struts2中提供了更为简单的参数请求与接收方法,可以直接在Action中定义属性:Struts2通过反射机制将参数反射到属性的set方法上实现参数的传递: GET方式传送参数 <strut ...

  6. maven项目打包

    配置 你的pom.xml文件,配置 packaging为 war ,然后点击 pom.xml右键,run as 选择 install 或是 package: 如果项目没问题,配置没问题,就会在项目的t ...

  7. OAuth 2.0 for MVC, Two Legged Implementation

    OAuth 2.0 for MVC, Two Legged Implementation tdupont  Fri, Mar 18 2011 9:30 AM  13 OAuth 1.0 was one ...

  8. C#去掉字符串中的汉字

    string str = "测试一下ilove中国so结束"; Regex reg = new Regex(@"[\u4e00-\u9fa5]"); Label ...

  9. Row versus Set Processing, Surprise!(集合处理和单行处理数据的差异性)

    Row versus Set Processing, Surprise! Craig Shallahamer: 1. Set based processing will likely be much ...

  10. 如何设置MySQL数据库名、表名大小写敏感

    在 MySQL 中,数据库和表其实就是数据目录下的目录和文. 因而,操作系统的敏感性决定数据库和表命名的大小写敏感.这就意味着数据库和表名在 Windows 中是大小写不敏感的,而在大多数类型的 Un ...