std::set::value_comp(3)

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

✨ std::set::value_comp ✨

Okay so bestie, welcome to the std::set::value_comp page β€” it's a C++ Standard Library one, from the <set> crew, and honestly it's a short one so we'll be in and out in a sec πŸ’…


🌸 SYNOPSIS 🌸

std::set::value_compare value_comp() const;

So what does it do? It returns the function object that compares the values. That's it. That's the whole vibe. And lowkey it's the exact same thing as key_comp. Twin behaviour, no cap 🫢


πŸŽ€ PARAMETERS πŸŽ€

  • (none) β€” not a single argument, she takes nothing, an icon of minimalism ✨

πŸ’– RETURN VALUE πŸ’–

The value comparison function object. That's the whole return, bestie β€” she hands you the comparator and minds her business 🌸


πŸ¦‹ COMPLEXITY πŸ¦‹

Constant. Literally O(1) energy, the fastest girl in the group chat ⭐


πŸ‘‘ EXAMPLE πŸ‘‘

Go ahead and run this code, it's giving "let me show you" πŸ™ˆ

// Run this code

#include <iostream>
#include <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::set<int, ModCmp> cont{1, 2, 3, 4, 5};

    // Same behaviour as 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 << "Key (" << key << ") ";
        if (!before && !after)
            std::cout << "equivalent to key (" << val << ")\n";
        else if (before)
            std::cout << "goes before key (" << val << ")\n";
        else if (after)
            std::cout << "goes after key (" << val << ")\n";
        else
            std::unreachable();
    }
}

And the Output is:

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)

The way key 3 and key 100 are equivalent because 3 % 97 and 100 % 97 both land on 3 β€” that's the ModCmp doing her little thing 🧚 iykyk πŸ’œ


🌈 SEE ALSO 🌈

  • key_comp β€” returns the function that compares keys (public member function)

Bestie reminder: nothing here is dangerous or deprecated, she's a well-behaved little member function, so you're safe 🫢

That's the whole page, diva. Stay hydrated, keep your comparators strict-weak-ordering, and remember: value_comp and key_comp are fr fr the same girl πŸ’…

Source: http://cppreference.com β€” version/date 2024.06.10 β€” and that's the std::set::value_comp(3) page, wrapped with love πŸŽ€βœ¨

πŸ“¦ where this page comes from