C++ multiset Library - key_comp() Function



Description

The multiset::key_comp() function in C++ STL returns a copy of the comparison object which is used by the multiset to compare keys. The returned comparison object defines the ordering of the elements in the multiset.

Syntax

Following is the syntax of multiset::key_comp() function −

key_compare multiset_name.key_comp() const;

Parameters

The key_comp() function does not accept any parameters.

Return value

The key_comp() function returns a copy of the comparison object used by the multiset. By default, it uses the less than(<) operator for comparison.

Exceptions

If an exception is thrown, the container remains unchanged.

Time complexity

The time complexity of multiset::key_comp() function is O(1) (constant time).

Examples of multiset::key_comp() Function

The following examples demonstrate the usage of multiset::key_comp() function in multiset −

Using key_comp() to Compare Elements

Below is an example to compare two integers (num1 and num2) in multiset using the comparison object returned by multiset::key_comp() function. The (comp(num1, num2)) checks num1 < num2.

#include <iostream>
#include <set>
using namespace std;

int main() {
    multiset<int> numbers = {10, 20, 30, 40, 50};
    cout << "Multiset elements:";
    for (const auto &num : numbers)
        cout << " " << num;
    cout << endl;

    auto comp = numbers.key_comp();
    int num1 = 25, num2 = 35;

    cout << "Comparing " << num1 << " with " << num2 << ":\n";
    if (comp(num1, num2))
        cout << num1 << " is less than " << num2 << endl;
    else
        cout << num1 << " is not less than " << num2 << endl;
    
    cout << "\nComparing " << num2 << " with " << num1 << ":\n";
    if (comp(num2, num1))
        cout << num2 << " is less than " << num1 << endl;
    else
        cout << num2 << " is not less than " << num1 << endl;

    return 0;
}

The output of the above code is given below −

Multiset elements: 10 20 30 40 50
Comparing 25 with 35:
25 is less than 35

Comparing 35 with 25:
35 is not less than 25

Printing Elements Less Than or Greater Than a Specific Key

You can use the comparison object returned by key_comp() to print all elements in the multiset that are less than or greater than a specific element. Here, we have printed all the elements that are less than and greater than the limit, which is 40.

#include <iostream>
#include <set>
using namespace std;

int main() {
    multiset<int> nums = {10, 40, 20, 30, 40, 50, 90, 35};
    cout << "Keys in multiset:";
    for (const auto &num : nums)
        cout << " " << num;
    cout << endl;

    auto comp = nums.key_comp();
    int limit = 40;

    cout << "Keys less than " << limit << " are: ";
    for (auto it = nums.begin(); it != nums.end(); ++it)
    {
        if (comp(*it, limit))
            cout << *it << " ";
        else
            break;
    }
    cout << endl;

    cout << "Keys greater than " << limit << " are: ";
    for (auto it = nums.begin(); it != nums.end(); ++it)
    {
        if (comp(limit, *it))
            cout << *it << " ";
    }
    cout << endl;

    return 0;
}

The output of the code is given below −

Keys in multiset: 10 20 30 35 40 40 50 90
Keys less than 40 are: 10 20 30 35 
Keys greater than 40 are: 50 90 

Using key_comp() to Compare Strings

In this example, we have compared two string elements of multiset using the key_comp() function −

#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {
    multiset<string> names = {"Aryan", "Bob", "Viper", "David"};

    cout << "Strings in Multiset: ";
    for (const auto &name : names)
        cout << " " << name;
    cout << endl;

    auto comp = names.key_comp();

    cout << "Checking if 'Viper' comes before 'Aryan': ";
    if (comp("Viper", "Aryan"))
        cout << "Yes, Viper comes before Aryan" << endl;
    else
        cout << "No, Aryan comes before Viper" << endl;

    return 0;
}

The output of the above code is given below −

Strings in Multiset:  Aryan Bob David Viper
Checking if 'Viper' comes before 'Aryan': No, Aryan comes before Viper
multiset.htm
Advertisements