Namespaces
Variants

std:: not2

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
not2
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
Défini dans l'en-tête <functional>
template < class Predicate >
std:: binary_negate < Predicate > not2 ( const Predicate & pred ) ;
(jusqu'à C++14)
template < class Predicate >
constexpr std:: binary_negate < Predicate > not2 ( const Predicate & pred ) ;
(depuis C++14)
(obsolète en C++17)
(supprimé en C++20)

std::not2 est une fonction utilitaire permettant de créer un objet fonction qui retourne le complément du prédicat binaire passé en paramètre. L'objet fonction créé est de type std:: binary_negate < Predicate > .

Le type de prédicat binaire doit définir deux types membres, first_argument_type et second_argument_type , qui sont convertibles vers les types de paramètres du prédicat. Les objets fonction obtenus à partir de 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 , ou d'un autre appel à std::not2 ont ces types définis, tout comme les objets fonction dérivés de l'obsolète std::binary_function .

Table des matières

Paramètres

pred - prédicat binaire

Valeur de retour

std::not2 retourne un objet de type std:: binary_negate < Predicate > , construit avec pred .

Exceptions

(aucun)

Exemple

#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 << "négation d'un 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 << "négation d'un foncteur standard:\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 << "négation d'une 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 << "négation d'un 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';
}

Sortie :

négation d'un binary_function :
0 2 true
1 1 false
2 0 true
négation d'un foncteur standard :
0 2 true
1 1 false
2 0 true
négation d'un std::function :
0 2 true
1 1 false
2 0 true
négation d'un std::reference_wrapper :
0 2 true
1 1 false
2 0 true

Voir aussi

(C++17)
crée un objet fonction qui retourne le complément du résultat de l'objet fonction qu'il contient
(modèle de fonction)
(obsolète en C++17) (supprimé en C++20)
objet fonction wrapper retournant le complément du prédicat binaire qu'il contient
(modèle de classe)
(C++11)
wrapper copiable de tout objet appelable constructible par copie
(modèle de classe)
wrapper non copiable de tout objet appelable qui supporte les qualificateurs dans une signature d'appel donnée
(modèle de classe)
(obsolète en C++17) (supprimé en C++20)
construit un objet std::unary_negate personnalisé
(modèle de fonction)
(obsolète en C++11) (supprimé en C++17)
crée un wrapper d'objet fonction compatible avec les adaptateurs à partir d'un pointeur de fonction
(modèle de fonction)
(obsolète en C++11) (supprimé en C++17)
classe de base de fonction binaire compatible avec les adaptateurs
(modèle de classe)