std::binary_function
來自 cppreference.com
定義於標頭檔案 <functional> |
||
template< class Arg1, |
(C++11 起已棄用) (在 C++17 中已移除) |
|
std::binary_function
是用於建立帶兩個引數的函式物件的基類。
std::binary_function
不定義 operator();預期派生類會定義它。std::binary_function
僅提供三個型別——first_argument_type
、second_argument_type
和 result_type
——由模板引數定義。
一些標準庫函式物件介面卡,例如 std::not2,要求它們適配的函式物件定義某些型別;std::not2 要求被適配的函式物件有兩個名為 first_argument_type
和 second_argument_type
的型別。從 std::binary_function
派生帶兩個引數的函式物件是使它們與這些介面卡相容的簡便方法。
std::binary_function
在 C++11 中已棄用,在 C++17 中已移除。
[編輯] 成員型別
型別 | 定義 |
first_argument_type
|
Arg1
|
second_argument_type
|
Arg2
|
result_type
|
結果
|
[編輯] 示例
執行此程式碼
#include <algorithm> #include <functional> #include <iostream> #include <vector> struct same : std::binary_function<int, int, bool> { bool operator()(int a, int b) const { return a == b; } }; int main() { std::vector<char> v1{'A', 'B', 'C', 'D', 'E'}; std::vector<char> v2{'E', 'D', 'C', 'B', 'A'}; std::vector<bool> v3(v1.size()); std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), std::not2(same())); std::cout << std::boolalpha; for (std::size_t i = 0; i < v1.size(); ++i) std::cout << v1[i] << " != " << v2[i] << " : " << v3[i] << '\n'; }
輸出
A != E : true B != D : true C != C : false D != B : true E != A : true
[編輯] 參閱
(C++11) |
任何可複製構造的可呼叫物件的包裝器 (類模板) |
(C++23) |
任何支援給定呼叫簽名中限定符的可呼叫物件的僅移動包裝器 (類模板) |
(C++11 中已廢棄)(C++17 中已移除) |
從函式指標建立介面卡相容函式物件包裝器 (函式模板) |
(C++11 中已廢棄)(C++17 中已移除) |
指向二元函式的指標的介面卡相容包裝器 (類模板) |
(C++11 中已廢棄)(C++17 中已移除) |
與介面卡相容的一元函式基類 (類模板) |