目錄
一、函數對象
1.函數對象的概念
2.函數對象的使用
(1)函數對象在使用的時候,可以像普通函數那樣調用,可以有參數,也可以有返回值。
(2)函數對象超出普通函數的概念,函數對象由于是類,可以有自己的狀態
(3)函數對象可以作為參數傳遞
二、謂詞
1.一元謂詞
2.二元謂詞
三、內建函數對象
1.算數仿函數
2.關系仿函數
3.邏輯仿函數
一、函數對象
1.函數對象的概念
重載函數調用運算符的類,也叫仿函數,本質是類而不是函數
2.函數對象的使用
(1)函數對象在使用的時候,可以像普通函數那樣調用,可以有參數,也可以有返回值。
(2)函數對象超出普通函數的概念,函數對象由于是類,可以有自己的狀態
(3)函數對象可以作為參數傳遞
既然他可以作為參數傳遞,所以很多STL的算法都有這個作為參數。
二、謂詞
對于仿函數,返回值類型是bool數據類型,稱為謂詞。
1.一元謂詞
如果operator()接收一個參數,那么叫做一元謂詞。所以說函數對象都是和算法一起使用的。
2.二元謂詞
如果operator()接收二個參數,那么叫做二元謂詞
三、內建函數對象
就是有很多函數對象常用,所以STL都給封裝好了,不需要自己寫了。需要引入頭文件#include<functional>。
1.算數仿函數
函數原型
template<<class T> T plus<T>? ?// 加法仿函數
template<<class T> T minus<T>? ?// 減法仿函數
template<<class T> T multiplies<T>? ?// 乘法仿函數
template<<class T> T divides<T>? ?// 除法仿函數
template<<class T> T modules<T>? ?// 取模仿函數
template<<class T> T negate<T>? ?// 取相反數仿函數
2.關系仿函數
template<<class T> bool equal_to<T>? ? ? ? ? ? //等于
template<<class T> bool not_equal_to<T>? ? ? ? ? ? //不等于
template<<class T> bool greater<T>? ? ? ? ? ? //不等于
template<<class T> bool greate_equal<T>? ? ? ? ? ? //不等于
template<<class T> bool less<T>? ? ? ? ? ? //不等于
template<<class T> bool less_equal<T>? ? ? ? ? ? //不等于
這里排序使用的仿函數就是STL自帶的了,而不是自己寫的了。
3.邏輯仿函數
template<<class T> bool logical_and<T>? ? ? // 邏輯與
template<<class T> bool logical_or<T>? ? ? // 邏輯或
template<<class T> bool logical_not<T>? ? ? // 邏輯非
?