Namespaces
Variants

std::multimap<Key,T,Compare,Allocator>:: key_comp

From cppreference.net

key_compare key_comp ( ) const ;
(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 <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::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 << "geht vor Schlüssel (100)\n";
        else if (after)
            std::cout << "geht nach Schlüssel (100)\n";
        else
            std::unreachable();
    }
}

Ausgabe:

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

Siehe auch

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