函数对象(functional)
如果f是一个function object,则可以将operator()作用于f身上。他是一个行为类似于函数的对象,为了能够行为类似函数,其类别中必须定义(或重载、重写)function call运算符(operator()),就可以在函数对象后加一对小括号以此来调用函数对象定义的operator()。
调用函数对象时构造函数和operator()执行顺序
- 首先执行构造函数,构造出一个匿名对象
- 然后在执行operator(),产生函数行为
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <iterator>
using namespace std; class F1
{
public:
F1(string t):s1(t)
{
cout<<" 带参构造函数"<<endl;
}
F1()
{
cout<<" 无参构造函数"<<endl;
}
bool operator()(string s)
{
cout<<" operator()函数"<<endl;
return strcmp(s.c_str(),s1.c_str());
}
private:
string s1;
};
int main()
{
vector<string> vs{"hello"," ","word","!","how"," ","you","."};
//1.F1 f1("you");被解析为f1.operator(arg);
F1 f1("you");
remove_copy_if(vs.begin(),vs.end(),ostream_iterator<string>(cout,"\n"),f1);
cout<<"****************************************"<<endl;
//2.
remove_copy_if(vs.begin(),vs.end(),ostream_iterator<string>(cout,"\n"),F1("you"));
cout<<"****************************************"<<endl;
//3.
F1()("hello");//这是一个函数调用的行为
return ;
}
函数对象可以有自己的状态,也可以与函数配接器搭配使用。
template<typename T, T add>
struct m_plus
{
m_plus() { _add = add; }
T operator()(const T& x) { return x + _add; }
// 仿函数可以具有自己的状态
int _add;
};
为了能够拥有配接能力,每一个仿函数必须定义自己的响应型别。这些型别是为了让配接器能够取出,获得仿函数的某些信息。
unary_function
来反应一元仿函数的参数型别和返回值型别。
//一元仿函数
template <class Arg,class Result>
struct unary_function{
typedef Arg argument_type;
typedef Result result_type;
};
binary_function
来反应二元仿函数的第一参数型别第二参数型别返回值型别。
//二元仿函数
template <class Arg1,class Arg2,Class Result>
struct binary_function{
typedef Arg1 firs_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
算数类仿函数
template <class T>
struct plus:public binary_function<T,T,T>{
T operator()(const T & x,const T & y) const {return x + y;}
}; template <class T>
struct minus:public binary_function<T,T,T>{
T operator()(const T & x,const T & y) const {return x - y;}
}; tmeplate<class T>
struct muliplies:public binary_function<T,T,T>{
T operator()(const T & x,const T & y) const {return x * y;}
}; tmeplate<class T>
struct divides:public binary_function<T,T,T>{
T operator()(const T & x,const T & y) const {return x / y;}
}; tmeplate<class T>
struct modulus:public binary_function<T,T,T>{
T operator()(const T & x,const T & y) const {return x % y;}
}; tmeplate<class T>
struct negate:public unary_function<T,T>{
T operator()(const T & x) const {return -x;}
};
证同元素
意思是数值A若与该元素做op运算,会得到A自己。例如加法的证同元素是0,任何元素加上0都是自己本身。乘法的证同元素是1,任何元素乘1都为元素本身。
template<class T>
inline T identity_element(plus<T>)
{return T();} template<class T>
inline T identity_element(multiplies<T>)
{return T();}
逻辑运算符仿函数
他们都继承与二元仿函数。
template<class T>
struct logical_and:public binary_function<T,T,bool>{
bool operator()(const T & x,const T & y) const {return x&&y;}
}; template<class T>
struct logical_or:public binary_function<T,T,bool>{
bool operator()(const T & x,const T & y) const {return x||y;}
}; template<class T>
struct logical_not:public unary_function<T,bool>{
bool operator()(const T & x) const {return !x;}
};
证同(identity)、选择(select)、投射(project)
//证同函数。任何数值通过此函数后,不会有任何改变
//此式运用于<stl_set.h>,用来指定RB-tree所需的KeyOfValue op
//set元素键值即实值,所以采用identity
template <class T>
struct identity : public unary_function<T,T>{
const T& operator()const T& x) const { return x; }
}; //选择函数:接收一个pair,返回其第一元素
//此式运用于<stl_map.h>,用来指定RB-tree所需的KeyOfValue op
//由于map系以pair元素的第一元素为其键值,所以采用select1st
template <class Pair>
struct select1st : public unary_function<Pair,typename Pair::first_type>
{
const typename Pair::first::first_type& operator()(const Pair& x)const{
return x.first;
}
}; //选择函数:接收一个Pair,传回其第二元素
//SGI STL未运用此式
template <class Pair>
struct select2nd : public unary_function<Pair,typename Pair::second_type>
{
const typename Pair::first::second_type& operator()(const Pair& x)const{
return x.second;
}
}; //投射函数:传回第一参数,忽略第二参数
//SGI STL未运用此式
template<class Arg1,class Arg2>
struct project1st : public binary_function<Arg1,Arg2,Arg1>{
Arg1 operator()(const Arg1& x,const Arg2&)const{ return x; }
}; //投射函数:传回第二参数,忽略第一参数
//SGI STL未运用此式
template<class Arg1,class Arg2>
struct project2nd : public binary_function<Arg1,Arg2,Arg2>{
Arg1 operator()(const Arg1&,const Arg2& y)const{ return y; }
};
函数对象(functional)的更多相关文章
- C++ Pirmer : 第十四章 : 重载运算符与类型转换之函数调用运算符与标准库的定义的函数对象
函数调用运算符 struct test { int operator()(int val) const { return (i > 0 ? i : -i); } }; 所谓的函数调用就是一个类重 ...
- C++11多态函数对象包装器
[C++11多态函数对象包装器] 针对函数对象的多态包装器(又称多态函数对象包装器)在语义和语法上和函数指针相似,但不像函数指针那么狭隘.只要能被调用,且其参数能与包装器兼容的都能以多态函数对象包装器 ...
- STL_iterator迭代器(3)——函数和函数对象
STL中,函数被称为算法,也就是说它们和标准C库函数相比,它们更为通用.STL算法通过重载operator()函数实现为模板类或模板函数.这些类用于创建函数对象,对容器中的数据进行各种各样的操作.下面 ...
- C++ Primer 学习笔记_62_重载操作符与转换 --调用操作符和函数对象
重载操作符与转换 --调用操作符和函数对象 引言: 能够为类类型的对象重载函数调用操作符:一般为表示操作的类重载调用操作符! struct absInt { int operator() (int v ...
- STL算法设计理念 - 预定义函数对象
预定义函数对象基本概念:标准模板库STL提前定义了很多预定义函数对象 1)使用预定义函数对象: #include <iostream> #include <cstdio> #i ...
- C++STL 预定义函数对象和函数适配器
预定义函数对象和函数适配器 预定义函数对象基本概念:标准模板库STL提前定义了很多预定义函数对象,#include <functional> 必须包含. 1使用预定义函数对象: void ...
- STL——仿函数(函数对象)
一.仿函数(也叫函数对象)概观 仿函数的作用主要在哪里?从第6章可以看出,STL所提供的各种算法,往往有两个版本,其中一个版本表现出最常用(或最直观)的某种运算,第二个版本则表现出最泛化的演算流程,允 ...
- 02--STL算法(函数对象和谓词)
一:函数对象(仿函数):实现状态记录等其他操作<相对于普通函数> 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象. 即是重载了“ ...
- STL 算法中函数对象和谓词
STL 算法中函数对象和谓词 函数对象和谓词定义 函数对象: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特 ...
随机推荐
- vijos 1423 最短路or环(有向图)
最佳路线 描述 年久失修的赛道令国际汽联十分不满.汽联命令主办方立即对赛道进行调整,否则将取消其主办权.主办方当然必须马上开始行动. 赛道测评人员经过了三天三夜的数据采集,选出了若干可以使用的道路和各 ...
- js判断数组,对象是否存在某一未知元素
1.对象 var obj = { aa:'1111', bb:'2222', cc: '3333' }; var str='aa'; if(str in obj){ console.log(obj[s ...
- BZOJ1300 [LLH邀请赛]大数计算器
一开始以为暴力搞,后来看了数据范围还以为要FFT,各种被虐,然后Orz Seter大神!!! 我只想到了前三位:a * b <=> 10^(log(a) + log(b)),于是把乘的数都 ...
- cas AssertionThreadLocalFilter
AssertionThreadLocalFilter AssertionThreadLocalFilter作用很简单,就是将Assertion绑定到ThreadLocal. ThreadLocal 无 ...
- learning armbian steps(2) ----- armbian 镜像编译
参考:https://docs.armbian.com/Developer-Guide_Build-Preparation/ 通过如下指令进行编译: apt-get -y -qq install gi ...
- js 实现智能输入数字
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- 玩转控件:重绘DEVEXPRESS中DateEdit控件 —— 让DateEdit支持只选择年月 (提供源码下载)
前言 上一篇博文<玩转控件:重绘ComboBox —— 让ComboBox多列显示>中,根据大家的回馈,ComboBox已经支持筛选了,更新见博文最后最后最后面. 奇葩 这两天遇到 ...
- James Whittaker:经营成功的测试职业生涯
转注:这篇文章出自 James A. Whittaker,Google的工程总监,负责Google部分产品的测试,包括Chrome.地图.GoogleWebApp.在加盟Google之前,James在 ...
- DevExpress v17.2新版亮点—Analytics Dashboard篇(一)
用户界面套包DevExpress v17.2日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了Analytics Dashboard v17.2 的新功能,快来下载试用新版本! ...
- Java实现责任链模式
责任链模式: 将接受者对象连成一条链,并在该链上传递请求,直到一个几首这对象处理它.通过让更多对象有机会处理请求,避免了请求发送者和接受者之间的耦合. 责任链模式的优缺点: 优点:高内聚,低耦合.业务 ...