C++ multiset Library - value_comp() Function



Description

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

In multiset, since keys and values are the same, the returned comparison object of value_comp() is same as key_comp().

Syntax

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

value_compare multiset_name.value_comp() const;

Parameters

The value_comp() function does not accept any parameters.

Return value

The value_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::value_comp() function is O(1) (constant time).

Examples of multiset::value_comp() Function

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

Using value_comp() to Compare Elements

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

#include <iostream>
#include <set>
using namespace std;
int main() {
    multiset<int> numbers = {15, 25, 35, 45, 55};
    cout << "Multiset elements:";
    for (const auto &num : numbers)
        cout << " " << num;
    cout << endl;
    auto comp = numbers.value_comp();
    int val1 = 30, val2 = 50;
    cout << "Comparing " << val1 << " with " << val2 << ":\n";
    if (comp(val1, val2))
        cout << val1 << " is less than " << val2 << endl;
    else
        cout << val1 << " is not less than " << val2 << endl;
    
    cout << "\nComparing " << val2 << " with " << val1 << ":\n";
    if (comp(val2, val1))
        cout << val2 << " is less than " << val1 << endl;
    else
        cout << val2 << " is not less than " << val1 << endl;
    return 0;
}

The output of the above code is given below −

Multiset elements: 15 25 35 45 55
Comparing 30 with 50:
30 is less than 50

Comparing 50 with 30:
50 is not less than 30

Printing Values Less Than or Greater Than a Specific Value

You can use the comparison object returned by value_comp() to print all values in the multiset that are less than or greater than a specific value. Here, we have printed all the values 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 << "Multiset values:";
    for (const auto &num : nums)
        cout << " " << num;
    cout << endl;

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

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

    cout << "Values 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 above code is given below −

Multiset values: 10 20 30 35 40 40 50 90
Values less than 40 are: 10 20 30 35 
Values greater than 40 are: 50 90

Using value_comp() to Compare Strings

In this example, we have compared two string elements of multiset using the value_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.value_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