std::set::value_comp(3)
β¨ 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
- 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