Open In App

multiset value_comp() method in C++ STL

Last Updated : 12 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The std::multiset::value_comp is an inbuilt function in C++ STL which returns a copy of the comparison object used by the container. By default, this is a less object, which returns the same as operator ‘<‘.It is a function pointer or a function object which takes two arguments of the same type as the container elements and returns true if the first argument is considered to go before the second in the strict weak ordering it defines or false otherwise. Two keys are considered equivalent if key_comp returns false reflexively (i.e., no matter the order in which the keys are passed as arguments). 

Syntax: 

value_compare multiset_name.value_comp() 

Parameters: This function does not accept any parameter. 

Return value: The function returns a copy of the comparison object used by the container. 

Below examples illustrate the above method: 

Example 1: 

CPP




// C++ program to illustrate the
// multiset::value_comp() function
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // Creating a multiset named m;
    multiset<int> m;
 
    multiset<int>::value_compare
        comp
        = m.value_comp();
 
    // Inserting elements into multiset
    m.insert(10);
    m.insert(20);
    m.insert(30);
    m.insert(40);
 
    cout << "Multiset has the elements\n";
 
    // Store key value of last element
    int highest = *m.rbegin();
 
    // initializing the iterator
    multiset<int>::iterator it = m.begin();
 
    // printing elements of all multiset
    do {
 
        cout << " " << *it;
 
    } while (comp(*it++, highest));
 
    return 0;
}


Output:

Multiset has the elements
 10 20 30 40

Example 2: 

CPP




// C++ program to illustrate the
// multiset::value_comp() function
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // Creating a multiset named m;
    multiset<int> m;
 
    multiset<int>::value_compare
        comp
        = m.value_comp();
 
    // Inserting elements into multiset
    m.insert(100);
    m.insert(200);
    m.insert(300);
    m.insert(400);
 
    cout << "Multiset has the elements\n";
 
    // Store key value of last element
    int highest = *m.rbegin();
 
    // initializing the iterator
    multiset<int>::iterator it = m.begin();
 
    // printing elements of all multiset
    do {
 
        cout << " " << *it;
 
    } while (comp(*it++, highest));
 
    return 0;
}


Output:

Multiset has the elements
 100 200 300 400

Time complexity: O(n). // n is the size of the multiset.
Auxiliary space: O(1).



Similar Reads

set value_comp() function in C++ STL
The set::value_comp() is an inbuilt function in cpp that returns a copy of the comparison object used by the container. This object determines the order of the elements in the container. It is a function pointer or a function object that takes two arguments of the same type as the container elements, and returns true if the first argument is consid
2 min read
multimap value_comp() function in C++ STL
The multimap::value_comp() method returns a comparison object that can be used to compare two elements to get whether the key of the first one goes before the second. Here the 1st object compares the object of type std::multimap::type. The arguments taken by this function object are of member type type. It is defined in multimap as an alias of pair
2 min read
map value_comp() in C++ STL
The std::map::value_comp() is a function in C++ STL. It returns a function object that compares objects of type std::map::value. Syntax: value_compare value_comp() const Parameters: It does not accept any parameters. Returns: This method returns a function object that compares objects of type std::map::value. Time Complexity: O(1) Below examples il
2 min read
multiset::operator= in C++ STL
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. multiset::operator= This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new contents.Syntax : multisetname1 = (multisetname2) Parameter
3 min read
multiset::swap() in C++ STL
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. multiset::swap() This function is used to exchange the contents of two multisets but the sets must be of same type, although sizes may differ. Syntax : multisetname1.swap(multisetname2) Parameters : The name of the multiset
3 min read
multiset::emplace() in C++ STL
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. multiset::emplace() This function is used to insert a new element into the multiset container. Syntax : multisetname.emplace(value) Parameters : The element to be inserted into the multiset is passed as the parameter. Result
3 min read
multiset lower_bound() in C++ STL with Examples
The multiset::lower_bound() is a built-in function in C++ STL which returns an iterator pointing to the first element in the container which is equivalent to k passed in the parameter. In case k is not present in the set container, the function returns an iterator pointing to the immediate next element which is just greater than k. If the key passe
3 min read
multiset upper_bound() in C++ STL with Examples
The multiset::upper_bound() is a built-in function in C++ STL that returns an iterator pointing to the immediate next element which is just greater than k. If the key passed in the parameter exceeds the maximum key in the container, then the iterator returned points an element which points to the position after the last element in the container. Sy
3 min read
multiset max_size() in C++ STL with Examples
The multiset::max_size() is a built-in function in C++ STL which returns the maximum number of elements a multiset container can hold. Syntax: multiset_name.max_size() Parameters: The function does not accept any parameters. Return Value: The function returns the maximum number of elements a multiset container can hold. Below programs illustrate th
1 min read
multiset insert() function in C++ STL
The multiset::insert() is a built-in function in C++ STL which insert elements in the multiset container or inserts the elements from a position to another position from one multiset to a different multiset. Syntax: iterator multiset_name.insert(element)Time Complexity: O(log n) Since the elements are always in sorted order, the newly inserted elem
4 min read
Practice Tags :