std::unary_function
來自 cppreference.com
定義於標頭檔案 <functional> |
||
template< typename ArgumentType, typename ResultType > struct unary_function; |
(自 C++11 起棄用) (在 C++17 中已移除) |
|
std::unary_function
是用於建立帶一個引數的函式物件的基類。
std::unary_function
不定義 operator();派生類應定義此運算子。std::unary_function
僅提供兩個型別——argument_type
和 result_type
——由模板引數定義。
某些標準庫函式物件介面卡,例如 std::not1,要求其所適配的函式物件定義某些特定型別;std::not1 要求被適配的函式物件擁有名為 argument_type
的型別。從 std::unary_function
派生帶一個引數的函式物件是使它們與這些介面卡相容的簡單方法。
std::unary_function
在 C++11 中已被棄用。
[編輯] 成員型別
型別 | 定義 |
argument_type
|
ArgumentType
|
result_type
|
ResultType
|
[編輯] 示例
執行此程式碼
#include <algorithm> #include <functional> #include <iostream> #include <vector> struct less_than_7 : std::unary_function<int, bool> { bool operator()(int i) const { return i < 7; } }; int main() { std::vector<int> v(10, 7); v[0] = v[1] = v[2] = 6; std::cout << std::count_if(v.begin(), v.end(), std::not1(less_than_7())); // C++11 solution: // Cast to std::function<bool (int)> somehow - even with a lambda // std::cout << std::count_if(v.begin(), v.end(), // std::not1(std::function<bool (int)>([](int i) { return i < 7; }))); }
輸出
7
[編輯] 參閱
(C++11) |
任何可複製構造的可呼叫物件的包裝器 (類模板) |
(C++23) |
任何支援給定呼叫簽名中限定符的可呼叫物件的僅移動包裝器 (類模板) |
(C++11 中已廢棄)(C++17 中已移除) |
從函式指標建立介面卡相容函式物件包裝器 (函式模板) |
(C++11 中已廢棄)(C++17 中已移除) |
指向一元函式的指標的介面卡相容包裝器 (類模板) |
(C++11 中已廢棄)(C++17 中已移除) |
與介面卡相容的二元函式基類 (類模板) |