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. C语言程序设计(翁恺)--第三周课件中的三个遗留点

    刚刚写完第二周遗留点,下面写第三周的 第三周:判断 1.if和else后面也可以没有{}而是一条语句.如果if后不带{},但是后面跟了两条语句,并且后面还有else语句,那么程序会怎么执行? 在Dev ...

  2. 移动网络山寨版(OpenBTS)【2】频段的故事

    OpenBTS 系统有两个看点.一个是无线收发,尤其是频段的处理,另一个是网络系统,尤其是替代传统的基站(BTS),基站控制器(BSC),移动控制中心(MSC),以及(HLR/VLR)的另类方案. 先 ...

  3. 访问动态链接库中的C++类和资源

    面我们来介绍如何访问动态链接库中的C++类和资源.其具体操作步骤如下:(1)创建一个基于对话框的工程,工程名称为“AccessDll”.设计对话框资源如图1所示. 图1  对话框资源设计窗口(2)定义 ...

  4. test for randomness

  5. floodlight 学习(一)

    其实这个控制器应该没有多少人用了吧,一年多都没更新了,鉴于最近无论如何都要用这个,将学习笔记贴出来吧. 1.FloodlightProvider(Dev) 1.1简介:FloodlightProvid ...

  6. [置顶] hdu4747 Mex 线段树

    题意:给你一个序列,让你求出对于所有区间<i, j>的mex和,mex表示该区间没有出现过的最小的整数. 思路:从时限和点数就可以看出是线段树,并且我们可以枚举左端点i, 然后求出所有左端 ...

  7. jQuery中的supersized的插件的功能描述

    Supersized特性: 自动等比例调整图片并填充整浏览器个屏幕. 循环展示图片,支持滑动和淡入淡出等多种图片切换效果. 导航按钮,支持键盘方向键导航. XHTML <div id=" ...

  8. Linux学习之十六、文件的格式化与相关处理

    原文地址:http://vbird.dic.ksu.edu.tw/linux_basic/0330regularex_4.php 文件的格式化与相关处理 接下来让我们来将文件进行一些简单的编排吧!底下 ...

  9. C# linq to xml

    XDocument doc = new XDocument( new XDeclaration("1.0", "utf-8", "yes") ...

  10. ssh框架用JUnit测试

    public class testAuxDict { //读spring配置文件 public static BeanFactory factory = new ClassPathXmlApplica ...