std::flat_m...t::key_comp(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ¦‹ NAME πŸ¦‹

okay so bestie, this is the std::flat_m...t::key_comp(3) page from the C++ Standard Libary manual β€” yes, the header title is literally std::flat_m...t::key_comp(3), and the NAME line is std::flat_multiset::key_comp - std::flat_multiset::key_comp. iconic. πŸ’…


✨ Synopsis ✨

the signature is literally this:

key_compare key_comp() const;  (since C++23)

it returns the function object that compares the keys, which is a copy of this container's constructor argument comp. it is the same as value_comp. no cap. 🫢


πŸŽ€ Parameters πŸŽ€

(none) β€” literally no parameters, no stress, periodt. πŸ¦‹


πŸ’– Return value πŸ’–

the key comparison function object. that's the whole return value, bestie β€” the key comparison function object. ✨


🌸 Complexity 🌸

Constant. that's it β€” constant. πŸ’…


πŸ”₯ Example πŸ”₯

okay this example is giving modulo-97 key comparison realness and I'm obsessed:

// Run this code

#include <iostream>
#include <flat_set>
#include <utility>

// Example module 97 key compare function
struct ModCmp
{
    bool operator()(int lhs, int rhs) const
    {
        return (lhs % 97) < (rhs % 97);
    }
};

int main()
{
    std::flat_multiset<int, ModCmp> cont{1, 2, 3, 4, 5};

    auto comp_func = cont.key_comp();

    for (const int key : cont)
    {
        const bool before = comp_func(key, 100);
        const bool after = comp_func(100, key);

        std::cout << '(' << key << ") ";
        if (!before && !after)
            std::cout << "equivalent to key (100)\n";
        else if (before)
            std::cout << "goes before key (100)\n";
        else if (after)
            std::cout << "goes after key (100)\n";
        else
            std::unreachable();
    }
}

πŸ“ Output: πŸ“

and the output is giving exactly this:

Key (1) goes before key (100)
Key (2) goes before key (100)
Key (3) equivalent to key (100)
Key (4) goes after key (100)
Key (5) goes after key (100)

🌈 See also 🌈

  • value_comp β€” returns the function that compares keys in objects of type value_type (public member function).

kisses and sparkles, this page is sourced from http://cppreference.com, dated 2024.06.10, and its footer title is std::flat_m...t::key_comp(3) πŸ’–πŸŽ€βœ¨

πŸ“¦ where this page comes from