#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 ...
随机推荐
- 二维树状数组(HD2642)
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<stdio.h> #include<string. ...
- SQL Server 查看实例配置情况的 2 方法
方法 1. sp_configure; execute sp_configure; 方法 2. sys.configurations select * from sys.configurations ...
- 论山寨手机与Android 【14】3G SmartPhone时代的MTK
分析了SmartPhone的里里外外以后,现在我们可以分析MTK的机遇和挑战了.MTK面临的外部环境在发生变化,变化有两条,一是移动网络从2G演变到3G,二是手机由FeaturePhone演化到Sma ...
- delphi SysErrorMessage 函数和系统错误信息表 good
在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示. 但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢? FormatMes ...
- yii post delete request more safe
常规的delete方法如下: /** * Deletes a particular model. * If deletion is successful, the browser will be r ...
- Codeforces 325D
#include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> ...
- 使用JS控制struts的日期控件datetimepicker
功能需求:页面主要有两个日历框,一个是当前日期,一个是去年同期,要求当用户改变当前日期时,同步修改去年同期为当前日期-1年. 当时刚接触到需求的第一时间想到的就是为< sx:datetimepi ...
- UVA 10003 Cutting Sticks 切木棍 dp
题意:把一根木棍按给定的n个点切下去,每次切的花费为切的那段木棍的长度,求最小花费. 这题出在dp入门这边,但是我看完题后有强烈的既是感,这不是以前做过的石子合并的题目变形吗? 题目其实就是把n+1根 ...
- windows开机启动nginx
1 .http://www.cuplayer.com/player/PlayerCode/Nginx/2014/0919/1577.html 2. http://www.cnblogs.com/xus ...
- javascript时间处理方法收集
首先收集到的是一个给某一个时间对象增加一段时间的方法, 例如2026-05-11增加一个月的时间,增加后时间为2026-05-11, 代码如下: function DateAdd(interval,n ...