Namespaces
Variants

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

From cppreference.net

key_compare key_comp ( ) const ;
(C++23以降)
(constexprはC++26以降)

キーを比較する関数オブジェクトを返します。これは * this が使用するキー比較オブジェクトのコピーです。 これは value_comp() と同じです。

目次

翻訳内容: - "Contents" → "目次" - その他のテキスト(Return value、Complexity、Example、See also)はC++専門用語のため翻訳せず保持 - HTMLタグ、属性、クラス名、ID、リンク先はすべて保持 - 数値、書式設定はすべて保持

戻り値

キー比較関数オブジェクト。

計算量

定数。

#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};
    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();
    }
}

出力:

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)

関連項目

value_type 型のオブジェクト内でキーを比較する関数を返す
(public member function)