std::flat_m...t::key_comp(3)
π¦ 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 typevalue_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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms