std::not1
來自 cppreference.com
定義於標頭檔案 <functional> |
||
template< class Predicate > std::unary_negate<Predicate> not1( const Predicate& pred ); |
(直到 C++14) | |
template< class Predicate > constexpr std::unary_negate<Predicate> not1( const Predicate& pred ); |
(C++14 起) (C++17 中已棄用) (C++20 中移除) |
|
std::not1
是一個輔助函式,用於建立一個函式物件,該物件返回傳遞給它的一元謂詞函式的補集。建立的函式物件型別為 std::unary_negate<Predicate>。
一元謂詞型別必須定義一個成員型別 argument_type
,該型別可轉換為謂詞的引數型別。從 std::ref、std::cref、std::negate、std::logical_not、std::mem_fn、std::function、std::hash,或從另一次對 std::not1
的呼叫獲得的一元函式物件都定義了此型別,以及從已棄用的 std::unary_function 派生的函式物件也是如此。
目錄 |
[編輯] 引數
pred | - | 一元謂詞 |
[編輯] 返回值
std::not1
返回一個型別為 std::unary_negate<Predicate> 的物件,用 pred 構造。
[編輯] 異常
(無)
[編輯] 示例
執行此程式碼
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> struct LessThan7 : std::unary_function<int, bool> { bool operator()(int i) const { return i < 7; } }; int main() { std::vector<int> v(10); std::iota(std::begin(v), std::end(v), 0); std::cout << std::count_if(begin(v), end(v), std::not1(LessThan7())) << '\n'; // the same as above using std::function std::function<bool(int)> less_than_9 = [](int x) { return x < 9; }; std::cout << std::count_if(begin(v), end(v), std::not1(less_than_9)) << '\n'; }
輸出
3 1
[編輯] 參閱
(C++17) |
建立函式物件,該物件返回其所持函式物件結果的補數 (函式模板) |
(C++17 中已棄用)(C++20 中已移除) |
返回其所持一元謂詞補集的包裝函式物件 (類模板) |
(C++11) |
任何可複製構造的可呼叫物件的包裝器 (類模板) |
(C++23) |
任何支援給定呼叫簽名中限定符的可呼叫物件的僅移動包裝器 (類模板) |
(C++17 中已棄用)(C++20 中已移除) |
構造自定義的 std::binary_negate 物件 (函式模板) |
(C++11 中已廢棄)(C++17 中已移除) |
從函式指標建立介面卡相容函式物件包裝器 (函式模板) |
(C++11 中已廢棄)(C++17 中已移除) |
與介面卡相容的一元函式基類 (類模板) |