名稱空間
變體
操作

std::not1

來自 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*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
not1
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
 
定義於標頭檔案 <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::refstd::crefstd::negatestd::logical_notstd::mem_fnstd::functionstd::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++17 中已棄用)(C++20 中已移除)
構造自定義的 std::binary_negate 物件
(函式模板) [編輯]
(C++11 中已廢棄)(C++17 中已移除)
從函式指標建立介面卡相容函式物件包裝器
(函式模板) [編輯]
(C++11 中已廢棄)(C++17 中已移除)
與介面卡相容的一元函式基類
(類模板) [編輯]