Namespaces
Variants

std::flat_multimap<Key,T,Compare,KeyContainer,MappedContainer>:: key_comp

From cppreference.net

key_compare key_comp ( ) const ;
(seit C++23)
(constexpr seit C++26)

Gibt das Funktionsobjekt zurück, das die Schlüssel vergleicht, welches eine Kopie des Schlüsselvergleichsobjekts ist, das von * this verwendet wird.

Inhaltsverzeichnis

Rückgabewert

Das Schlüsselvergleichsfunktionsobjekt.

Komplexität

Konstante.

Beispiel

#include <iostream>
#include <flat_map>
#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_multimap<int, char, ModCmp> cont;
    cont = {{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}, {5, 'e'}};
    auto comp_func = cont.key_comp();
    for (const auto it : cont)
    {
        const bool before = comp_func(it.first, 100);
        const bool after = comp_func(100, it.first);
        std::cout << "Schlüssel (" << it.first << ',' << it.second << ") ";
        if (!before && !after)
            std::cout << "äquivalent zu Schlüssel (100)\n";
        else if (before)
            std::cout << "kommt vor Schlüssel (100)\n";
        else if (after)
            std::cout << "kommt nach Schlüssel (100)\n";
        else
            std::unreachable();
    }
}

Ausgabe:

(1,a) kommt vor Schlüssel (100)
(2,b) kommt vor Schlüssel (100)
(3,c) äquivalent zu Schlüssel (100)
(4,d) kommt nach Schlüssel (100)
(5,e) kommt nach Schlüssel (100)

Siehe auch

gibt die Funktion zurück, die Schlüssel in Objekten vom Typ value_type vergleicht
(öffentliche Elementfunktion)