The value_comp function in C++ Multiset returns a comparison object that is utilized to determine if the key of the first element precedes the key of the second element during comparison.
It accepts two arguments of identical data type and evaluates whether the first argument precedes the second argument based on the narrower weak order, returning true if it does and false if it doesn't.
For a multiset denoted as m, when considering two elements e1(k1, d1) and e2(k2, d2) as instances of type valuetype, where k1 and k2 represent their keys of type keytype and d1 and d2 are their associated data, the expression m valuecomp(e1, e2) is equal to m keycomp(k1, k2).
Syntax
value_compare value_comp() const;
Note: A stored object defines a member function:
bool-operator (value_type &left, value_type &right);
It evaluates as true when the value associated with the left key comes before and is not equal to the value linked with the right key in the sorting sequence.
Parameter
Return value
It returns a value comparison function object.
Complexity
Constant.
Iterator validity
No changes.
Data Races
The container is accessed.
Accessing the elements within a multiset concurrently poses no risk as no contained elements are accessed.
Exception Safety
If an error is raised, the contents within the container remain unaltered.
Example 1
Let's examine a straightforward example to contrast the values of elements:
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> c;
multiset<int>::value_compare comp = c.value_comp();
cout << "Compare 2 to 5 (1 is true and 0 is false): "<<comp(2, 5) << endl;
cout << "Compare 8 to 5 (1 is true and 0 is false): "<<comp(8, 5) << endl;
return 0;
}
Output:
Compare 2 to 5 (1 is true and 0 is false): 1
Compare 8 to 5 (1 is true and 0 is false): 0
In the previous instance, when comp(2, 5) is called, it evaluates to true because 2 is smaller than 5. Similarly, when comp(8, 5) is executed, it results in false since 8 is not less than 5.
Example 2
Let's see a simple example:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset;
multiset<int>::value_compare mycomp = mymultiset.value_comp();
for (int i=0; i<=5; i++) mymultiset.insert(i);
cout << "mymultiset contains:";
int highest=*mymultiset.rbegin();
multiset<int>::iterator it=mymultiset.begin();
do {
cout << ' ' << *it;
} while ( mycomp(*(++it),highest) );
cout << '\n';
return 0;
}
Output:
mymultiset contains: 0 1 2 3 4
In the provided scenario, the variable "highest" retains the final element of the "mymultiset" container, which is a multiset. The iterator is set to the initial element of the multiset, following its sorted sequence. A do-while loop is employed to display the elements of the multiset. This loop continues to iterate as long as the key of the first element is smaller than the key of the last element. This comparison is facilitated by using the "key_comp" function referred to as "mycomp".
Example 3
Let's see a simple example:
#include <set>
#include <iostream>
int main( )
{
using namespace std;
multiset <int, less<int> > s1;
multiset <int, less<int> >::value_compare vc1 = s1.value_comp( );
bool result1 = vc1( 2, 3 );
if( result1 == true )
{
cout << "vc1( 2,3 ) returns value of true, "
<< "where vc1 is the function object of s1."
<< endl;
}
else
{
cout << "vc1( 2,3 ) returns value of false, "
<< "where vc1 is the function object of s1."
<< endl;
}
multiset <int, greater<int> > s2;
multiset<int, greater<int> >::value_compare vc2 = s2.value_comp( );
bool result2 = vc2( 2, 3 );
if( result2 == true )
{
cout << "vc2( 2,3 ) returns value of true, "
<< "where vc2 is the function object of s2."
<< endl;
}
else
{
cout << "vc2( 2,3 ) returns value of false, "
<< "where vc2 is the function object of s2."
<< endl;
}
}
Output:
vc1( 2,3 ) returns value of true, where vc1 is the function object of s1.
vc2( 2,3 ) returns value of false, where vc2 is the function object of s2.
Example 4
Let's examine a basic illustration to demonstrate the contrast between keycomp and valuecomp:
#include <set>
#include <iostream>
using namespace std;
int main(){
multiset<int> mymultiset;
int highest1, highest2;
multiset<int>::key_compare myCompKeyForMultiset = mymultiset.key_comp();
multiset<int>::value_compare myCompValForMultiset = mymultiset.value_comp();
for (int i=0; i<=5; i++) {
mymultiset.insert(i);
}
highest1=*mymultiset.rbegin();
multiset<int>::iterator it=mymultiset.begin();
while ( myCompKeyForMultiset(*it, highest1) ) it++;
cout << "\nhighest1 is " << highest1; // prints 5
highest2 = *mymultiset.rbegin();
it=mymultiset.begin();
while ( myCompValForMultiset(*it, highest2) ) it++;
cout << "\nhighest2 is " << highest2; // prints 5
return 0;
}
Output:
highest1 is 5
highest2 is 5
When comparing keycomp and valuecomp, in the context of multiset containers, these two functions are identical. They both return the same value regardless of the function type.