Namespaces
Variants

std::set<Key,Compare,Allocator>:: value_comp

From cppreference.net

value_compare value_comp ( ) const ;
(自 C++26 起为 constexpr)

返回用于比较值的函数对象。这与 key_comp() 相同。

目录

返回值

值比较函数对象。

复杂度

常量。

示例

#include <iostream>
#include <set>
#include <utility>
// 示例模块97键比较函数
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};
    // 与 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 << ") ";
        if (!before && !after)
            std::cout << "等价于键 (" << val << ")\n";
        else if (before)
            std::cout << "位于键 (" << val << ") 之前\n";
        else if (after)
            std::cout << "位于键 (" << val << ") 之后\n";
        else
            std::unreachable();
    }
}

输出:

键 (1) 位于键 (100) 之前
键 (2) 位于键 (100) 之前
键 (3) 等价于键 (100)
键 (4) 位于键 (100) 之后
键 (5) 位于键 (100) 之后

参见

返回用于比较键的函数
(公开成员函数)