名稱空間
變體
操作

std::unary_negate

來自 cppreference.com
 
 
 
函式物件
函式呼叫
(C++17)(C++23)
恆等函式物件
(C++20)
透明運算子包裝器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

舊繫結器和介面卡
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
unary_negate
(直到 C++20*)
(直到 C++20*)
 
定義於標頭檔案 <functional>
template< class Predicate >
struct unary_negate : public std::unary_function<Predicate::argument_type, bool>;
(C++11 前)
template< class Predicate >
struct unary_negate;
(C++11 起)
(C++17 中已棄用)
(C++20 中移除)

std::unary_negate 是一個包裝函式物件,返回其持有的一元謂詞的補碼。

一元謂詞型別必須定義一個成員型別 `argument_type`,它可轉換為謂詞的引數型別。從 std::refstd::crefstd::negatestd::logical_notstd::mem_fnstd::functionstd::hash 或另一次呼叫 std::not1 獲得的函式物件都定義了此型別,從已棄用的 std::unary_function 派生的函式物件也是如此。

使用輔助函式 std::not1 可以輕鬆構造 `std::unary_negate` 物件。

目錄

[編輯] 成員型別

型別 定義
argument_type Predicate::argument_type
result_type bool

[編輯] 成員函式

(建構函式)
構造一個新的 unary_negate 物件,並帶有所提供的謂詞
(公開成員函式)
operator()
返回對儲存的謂詞呼叫的結果的邏輯補碼
(公開成員函式)

std::unary_negate::unary_negate

explicit unary_negate( Predicate const& pred );
(直到 C++14)
constexpr explicit unary_negate( Predicate const& pred );
(C++14 起)

構造一個帶有儲存謂詞 pred 的 `std::unary_negate` 函式物件。

引數

pred - 謂詞函式物件

std::unary_negate::operator()

bool operator()( argument_type const& x ) const;
(直到 C++14)
constexpr bool operator()( argument_type const& x ) const;
(C++14 起)

返回呼叫 pred(x) 結果的邏輯補碼。

引數

x - 要傳遞給謂詞的引數

返回值

呼叫 pred(x) 結果的邏輯補碼。

[編輯] 示例

#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(7, 7);
    v[0] = v[1] = v[2] = 6;
 
    std::unary_negate<less_than_7> not_less_than_7((less_than_7()));
    // C++11 solution:
    // Use std::function<bool (int)>
    // std::function<bool (int)> not_less_than_7 =
    //     [](int x)->bool { return !less_than_7()(x); };
 
    std::cout << std::count_if(v.begin(), v.end(), not_less_than_7);
}

輸出

4

[編輯] 參閱

(C++17 中已棄用)(C++20 中已移除)
返回其所持二元謂詞補集的包裝函式物件
(類模板) [編輯]
(C++11)
任何可複製構造的可呼叫物件的包裝器
(類模板) [編輯]
任何支援給定呼叫簽名中限定符的可呼叫物件的僅移動包裝器
(類模板) [編輯]
(C++17 中已棄用)(C++20 中已移除)
構造自定義 std::unary_negate 物件
(函式模板) [編輯]
(C++11 中已廢棄)(C++17 中已移除)
從函式指標建立介面卡相容函式物件包裝器
(函式模板) [編輯]
(C++11 中已廢棄)(C++17 中已移除)
與介面卡相容的一元函式基類
(類模板) [編輯]