std::flat_multiset<Key,Compare,KeyContainer>::value_comp

来自cppreference.com

 
 
 
 
std::flat_multiset::value_compare value_comp() const;
(C++23 起)

返回比较值的函数对象。它与 key_comp 相同。

参数

(无)

返回值

比较值的函数对象。

复杂度

常数。

示例

#include <iostream>
#include <flat_set>
#include <utility>
 
// 演示模 97 的键比较函数
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};
 
    // 行为与 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 << key << " 等价于键 (" << val << ")\n";
        else if (before)
            std::cout << key << " 在键 (" << val << ") 之前\n";
        else if (after)
            std::cout << key << " 在键 (" << val << ") 之后\n";
        else
            std::unreachable();
    }
}

输出:

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

参阅

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