flat_multimap_key_comp

flat_multimap_key_comp (std::flat_multimap::key_comp — 키 비교 함수)

std::flat_multimap이 키를 정렬·비교하는 데 쓰는 함수 객체를 반환하는 멤버 함수예요. 이 컨테이너의 생성자 인자 comp의 복사본이에요.

출처: cppreference

본문

시그니처는 다음과 같아요.

key_compare key_comp() const;   // (since C++23)

키를 비교하는 함수 객체를 반환해요. 이 컨테이너의 생성자 인자 comp의 복사본이에요.

반환값

키 비교 함수 객체.

복잡도

상수(constant)예요.

예제

#include <iostream>
#include <flat_map>
#include <utility>
// 모듈로 97 키 비교 함수 예시
struct ModCmp
{
    bool operator()(int lhs, int rhs) const
    {
        return (lhs % 97) < (rhs % 97);
    }
};
int main()
{
    std::flat_multimap<int, char, ModCmp> cont;
    cont = {{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}, {5, 'e'}};
    auto comp_func = cont.key_comp();
    for (const auto it : cont)
    {
        const bool before = comp_func(it.first, 100);
        const bool after = comp_func(100, it.first);
        std::cout << "Key (" << it.first << ',' << it.second << ") ";
        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();
    }
}

출력:

(1,a) goes before key (100)
(2,b) goes before key (100)
(3,c) equivalent to key (100)
(4,d) goes after key (100)
(5,e) goes after key (100)

함께 보기

  • value_comp: value_type 객체의 키를 비교하는 함수를 반환해요 (공개 멤버 함수)

더 알아보기 (Learn more)

cppreference