std::not2
來自 cppreference.com
定義於標頭檔案 <functional> |
||
template< class Predicate > std::binary_negate<Predicate> not2( const Predicate& pred ); |
(until C++14) | |
template< class Predicate > constexpr std::binary_negate<Predicate> not2( const Predicate& pred ); |
(C++14 起) (C++17 中已棄用) (C++20 中移除) |
|
std::not2
是一個輔助函式,用於建立一個函式物件,該物件返回傳入的二元謂詞函式的補。建立的函式物件型別為 std::binary_negate<Predicate>。
二元謂詞型別必須定義兩個成員型別,first_argument_type
和 second_argument_type
,它們可以轉換為謂詞的引數型別。從 std::owner_less、std::ref、std::cref、std::plus、std::minus、std::multiplies、std::divides、std::modulus、std::equal_to、std::not_equal_to、std::greater、std::less、std::greater_equal、std::less_equal、std::logical_not、std::logical_or、std::bit_and、std::bit_or、std::bit_xor、std::mem_fn、std::map::value_comp、std::multimap::value_comp、std::function 或從另一個對 std::not2
的呼叫獲得的函式物件都定義了這些型別,從已棄用的 std::binary_function 派生的函式物件也是如此。
目錄 |
[edit] 引數
pred | - | 二元謂詞 |
[edit] 返回值
std::not2
返回型別為 std::binary_negate<Predicate> 的物件,使用 pred 構造。
[edit] 異常
(無)
[edit] 示例
執行此程式碼
#include <algorithm> #include <cstddef> #include <functional> #include <iostream> #include <vector> struct old_same : std::binary_function<int, int, bool> { bool operator()(int a, int b) const { return a == b; } }; struct new_same { bool operator()(int a, int b) const { return a == b; } }; bool same_fn(int a, int b) { return a == b; } int main() { std::vector<int> v1{0, 1, 2}; std::vector<int> v2{2, 1, 0}; std::vector<bool> v3(v1.size()); std::cout << "negating a binary_function:\n"; std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), std::not2(old_same())); std::cout << std::boolalpha; for (std::size_t i = 0; i < v1.size(); ++i) std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n'; std::cout << "negating a standard functor:\n"; std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), std::not2(std::equal_to<int>())); for (std::size_t i = 0; i < v1.size(); ++i) std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n'; std::cout << "negating a std::function:\n"; std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), std::not2(std::function<bool(int, int)>(new_same()))); for (std::size_t i = 0; i < v1.size(); ++i) std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n'; std::cout << "negating a std::reference_wrapper:\n"; std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), std::not2(std::ref(same_fn))); for (std::size_t i = 0; i < v1.size(); ++i) std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n'; }
輸出
negating a binary_function: 0 2 true 1 1 false 2 0 true negating a standard functor: 0 2 true 1 1 false 2 0 true negating a std::function: 0 2 true 1 1 false 2 0 true negating a std::reference_wrapper: 0 2 true 1 1 false 2 0 true
[edit] 參閱
(C++17) |
建立函式物件,該物件返回其所持函式物件結果的補數 (函式模板) |
(C++17 中已棄用)(C++20 中已移除) |
返回其所持二元謂詞補集的包裝函式物件 (類模板) |
(C++11) |
任何可複製構造的可呼叫物件的包裝器 (類模板) |
(C++23) |
任何支援給定呼叫簽名中限定符的可呼叫物件的僅移動包裝器 (類模板) |
(C++17 中已棄用)(C++20 中已移除) |
構造自定義的 std::unary_negate 物件 (函式模板) |
(C++11 中已廢棄)(C++17 中已移除) |
從函式指標建立介面卡相容函式物件包裝器 (函式模板) |
(C++11 中已廢棄)(C++17 中已移除) |
與介面卡相容的二元函式基類 (類模板) |