Namespaces
Variants

std::flat_set<Key,Compare,KeyContainer>:: value_comp

From cppreference.net

value_compare value_comp ( ) const ;
(seit C++23)
(constexpr seit C++26)

Gibt das Funktionsobjekt zurück, das die Werte vergleicht. Es ist dasselbe wie key_comp() .

Inhaltsverzeichnis

Rückgabewert

Das Wertvergleichsfunktionsobjekt.

Komplexität

Konstante.

Beispiel

#include <iostream>
#include <flat_set>
#include <utility>
// Beispiel-Modul-97-Schlüsselvergleichsfunktion
struct ModCmp
{
    bool operator()(int lhs, int rhs) const
    {
        return (lhs % 97) < (rhs % 97);
    }
};
int main()
{
    std::flat_set<int, ModCmp> cont{1, 2, 3, 4, 5};
    // Gleiches Verhalten wie key_comp()
    auto comp_func = cont.value_comp();
    for (const int val{100}; const int key : cont)
    {
        const bool before = comp_func(key, val);
        const bool after = comp_func(val, key);
        std::cout << "Schlüssel (" << key << ") ";
        if (!before && !after)
            std::cout << "äquivalent zu Schlüssel (" << val << ")\n";
        else if (before)
            std::cout << "kommt vor Schlüssel (" << val << ")\n";
        else if (after)
            std::cout << "kommt nach Schlüssel (" << val << ")\n";
        else
            std::unreachable();
    }
}

Ausgabe:

Schlüssel (1) kommt vor Schlüssel (100)
Schlüssel (2) kommt vor Schlüssel (100)
Schlüssel (3) äquivalent zu Schlüssel (100)
Schlüssel (4) kommt nach Schlüssel (100)
Schlüssel (5) kommt nach Schlüssel (100)

Siehe auch

gibt die Funktion zurück, die Schlüssel vergleicht
(öffentliche Elementfunktion)