Spec-Zone.ru › C++

std::multiset<Key,Compare,Allocator>::key_comp

key_compare key_comp() const;

Возвращает объект функции, сравнивающий ключи, который является копией аргумента конструктора этого контейнера конструктора comp. Он эквивалентен value_comp.

Параметры

(нет)

Значение, возвращаемое функцией

Объект функции сравнения ключей.

Сложность

Постоянная.

Пример

#include <cassert>
#include <iostream>
#include <set>
 
// Example module 97 key compare function
struct ModCmp
{
    bool operator()(const int lhs, const int rhs) const
    {
        return (lhs % 97) < (rhs % 97);
    }
};
 
int main()
{
    std::multiset<int, ModCmp> cont{1, 2, 3, 4, 5};
 
    auto comp_func = cont.key_comp();
 
    for (int key : cont)
    {
        bool before = comp_func(key, 100);
        bool after = comp_func(100, key);
        if (!before && !after)
            std::cout << key << " equivalent to key 100\n";
        else if (before)
            std::cout << key << " goes before key 100\n";
        else if (after)
            std::cout << key << " goes after key 100\n";
        else
            assert(0); // Cannot happen
    }
}

Вывод:

1 goes before key 100
2 goes before key 100
3 equivalent to key 100
4 goes after key 100
5 goes after key 100

См. также

value_comp
возвращает функцию, сравнивающую ключи в объектах типа value_type
(публичный член-функция)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/container/multiset/key_comp

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API