名稱空間
變體
操作

std::not2

來自 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*)
not2
(直到 C++20*)
(直到 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*)
 
定義於標頭檔案 <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_typesecond_argument_type,它們可以轉換為謂詞的引數型別。從 std::owner_lessstd::refstd::crefstd::plusstd::minusstd::multipliesstd::dividesstd::modulusstd::equal_tostd::not_equal_tostd::greaterstd::lessstd::greater_equalstd::less_equalstd::logical_notstd::logical_orstd::bit_andstd::bit_orstd::bit_xorstd::mem_fnstd::map::value_compstd::multimap::value_compstd::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++17 中已棄用)(C++20 中已移除)
構造自定義的 std::unary_negate 物件
(函式模板) [編輯]
(C++11 中已廢棄)(C++17 中已移除)
從函式指標建立介面卡相容函式物件包裝器
(函式模板) [編輯]
(C++11 中已廢棄)(C++17 中已移除)
與介面卡相容的二元函式基類
(類模板) [編輯]