C++ functional庫中的仿函數
一、仿函數簡介
仿函數(functor)又稱之為函數對象(function object),實際上就是 重載了()操作符 的 struct或class。
由於重載了()操作符,所以使用他的時候就像在調用函數一樣,於是就被稱為「仿」函數啦。
二、仿函數簡要寫法示例
一個很正常的需求,定義一個仿函數作為一個數組的排序規則:
將數組從大到小排序
class Cmp {
public:
bool operator()(const int &a, const int &b) {
return a > b;
}
};
使用:
vector<int> a(10);
iota(begin(a), end(a), 1);
sort(begin(a), end(a), Cmp()); // 使用()
for (auto x : a) {
cout << x << " ";
}
輸出:
10 9 8 7 6 5 4 3 2 1
三、使用C++自帶的仿函數
在C++ 的functional頭文件中,已經為我們提供好了一些仿函數,可以直接使用。
(1)算術仿函數
1.plus 計算兩數之和
例:將兩個等長數組相加
vector<int> a(10), b(a);
iota(begin(a), end(a), 1);
iota(begin(b), end(b), 1);
transform(begin(a), end(a), begin(b), begin(a), plus<int>());
for (auto x : a) {
cout << x << " ";
}
輸出:
2 4 6 8 10 12 14 16 18 20
2.minus 兩數相減
將上面那個例子改一改:
transform(begin(a), end(a), begin(b), begin(a), minus<int>());
輸出:
0 0 0 0 0 0 0 0 0 0
3.multiplies 兩數相乘
再將上面那個例子改一改:
transform(begin(a), end(a), begin(b), begin(a), multiplies<int>());
輸出:
1 4 9 16 25 36 49 64 81 100
4.divides 兩數相除
還將上面那個例子改一改:
transform(begin(a), end(a), begin(b), begin(a), divides<int>());
輸出:
1 1 1 1 1 1 1 1 1 1
5.modules 取模運算
繼續將上面那個例子改一改:
transform(begin(a), end(a), begin(b), begin(a), modulus<int>());
輸出:
0 0 0 0 0 0 0 0 0 0
6.negate 相反數
這次不能那樣改了,因為上述的五個仿函數是二元仿函數,是對兩個操作數而言的。
negate是一元仿函數,只能對一個參數求相反數。
所以我們對a數組求相反數:
transform(begin(a), end(a), begin(a), negate<int>());
輸出:
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
(2)關係仿函數
1.equal_to 是否相等
2.not_equal_to 是否不相等
3.greater 大於
4.less 小於
5.greater_equal 大於等於
6.less_equal 小於等於
到這時,我們就可以看出,可以使用 greater
將數組從大到小排序:
vector<int> a(10);
iota(begin(a), end(a), 1);
sort(begin(a), end(a), greater<int>()); // 使用()
for (auto x : a) {
cout << x << " ";
}
輸出:
10 9 8 7 6 5 4 3 2 1
(3)邏輯仿函數
1.logical_and 二元,求&
2.logical_or 二元,求|
3.logical_not 一元,求!
使用方法同上.
話說,並沒有發現求異或的仿函數..