std::binary_negate
定義於標頭檔案 <functional> |
||
template< class Predicate > struct binary_negate |
(C++11 前) | |
template< class Predicate > struct binary_negate; |
(C++11 起) (C++17 中已棄用) (C++20 中移除) |
|
std::binary_negate
是一個包裝函式物件,返回其持有的二元謂詞的補碼。
二元謂詞型別必須定義兩個成員型別,`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 派生的函式物件也是如此。
std::binary_negate
物件可以透過輔助函式 std::not2 輕鬆構建。
目錄 |
[編輯] 成員型別
型別 | 定義 |
first_argument_type
|
Predicate::first_argument_type |
second_argument_type
|
Predicate::second_argument_type |
result_type
|
bool |
[編輯] 成員函式
(建構函式) |
使用提供的謂詞構造一個新的 binary_negate 物件 (公開成員函式) |
operator() |
返回對儲存的謂詞呼叫的結果的邏輯補碼 (公開成員函式) |
std::binary_negate::binary_negate
explicit binary_negate( Predicate const& pred ); |
(直到 C++14) | |
constexpr explicit binary_negate( Predicate const& pred ); |
(C++14 起) | |
構造一個帶有儲存謂詞 pred 的 std::binary_negate
函式物件。
引數
pred | - | 謂詞函式物件 |
std::binary_negate::operator()
bool operator()( first_argument_type const& x, second_argument_type const& y ) const; |
(直到 C++14) | |
constexpr bool operator()( first_argument_type const& x, second_argument_type const& y ) const; |
(C++14 起) | |
返回呼叫 pred(x, y) 結果的邏輯補碼。
引數
x | - | 要傳遞給謂詞的第一個引數 |
y | - | 要傳遞給謂詞的第二個引數 |
返回值
呼叫 pred(x, y) 結果的邏輯補碼。
[編輯] 示例
#include <algorithm> #include <cstddef> #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<int> v1; for (int i = 0; i < 7; ++i) v1.push_back(i); std::vector<int> v2(v1.size()); std::reverse_copy(v1.begin(), v1.end(), v2.begin()); std::vector<bool> v3(v1.size()); std::binary_negate<same> not_same((same())); // C++11 solution: // std::function<bool (int, int)> not_same = // [](int x, int y) -> bool { return !same()(x, y); }; std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), not_same); std::cout.setf(std::ios_base::boolalpha); for (std::size_t i = 0; i != v1.size(); ++i) std::cout << v1[i] << " != " << v2[i] << " : " << v3[i] << '\n'; }
輸出
0 != 6 : true 1 != 5 : true 2 != 4 : true 3 != 3 : false 4 != 2 : true 5 != 1 : true 6 != 0 : true
[編輯] 參閱
(C++11 中已廢棄)(C++17 中已移除) |
與介面卡相容的二元函式基類 (類模板) |
(C++11) |
任何可複製構造的可呼叫物件的包裝器 (類模板) |
(C++23) |
任何支援給定呼叫簽名中限定符的可呼叫物件的僅移動包裝器 (類模板) |
(C++17 中已棄用)(C++20 中已移除) |
構造自定義 std::binary_negate 物件 (函式模板) |
(C++11 中已廢棄)(C++17 中已移除) |
從函式指標建立介面卡相容函式物件包裝器 (函式模板) |
(C++17 中已棄用)(C++20 中已移除) |
返回其所持一元謂詞補集的包裝函式物件 (類模板) |