C++ set value_comp
The value_comp function in C++ provides a comparison object. It is employed to evaluate if the key of the first element precedes the key of the second element during comparison.
It accepts two parameters of identical data type and outputs true if the first parameter comes before the second parameter based on the more specific weak order, or false if not.
For a set \( m \), if there are two elements \( e1(k1, d1) \) and \( e2(k2, d2) \) which are instances of type \( value\type \), where \( k1 \) and \( k2 \) represent their keys of type \( key\type \) and \( d1 \) and \( d2 \) represent their data of type \( setped\type \), then \( m \; value\comp(e1, e2) \) is the same as \( m \; key\_comp(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 of the left key comes before, but is not equal to, the value of 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 of a set concurrently is secure as no elements within the set are modified.
Exception Safety
If an error is raised, the container remains unchanged.
Example 1
Let's examine a basic example to contrast the values of elements:
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> c;
set<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;
}
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 given illustration, calling comp(2, 5) results in true since 2 is smaller than 5. Conversely, comp(8, 5) yields false as 8 is not less than 5.
Example 2
Let's see a simple example:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<int> myset;
set<int>::value_compare mycomp = myset.value_comp();
for (int i=0; i<=5; i++) myset.insert(i);
cout << "myset contains:";
int highest=*myset.rbegin();
set<int>::iterator it=myset.begin();
do {
cout << ' ' << *it;
} while ( mycomp(*(++it),highest) );
cout << '\n';
return 0;
}
Output:
myset contains: 0 1 2 3 4
The variable "highest" in the given scenario contains the final element of the "myset" set, while the iterator is set to the initial element of the set (sorted). A do-while loop is implemented to display the set's elements, with the loop iterating until the first key is smaller than the last key. 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;
set <int, less<int> > s1;
set <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;
}
set <int, greater<int> > s2;
set<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>
#include<map>
using namespace std;
int main(){
set<int> myset;
int highest1, highest2;
set<int>::key_compare myCompKeyForSet = myset.key_comp();
set<int>::value_compare myCompValForSet = myset.value_comp();
for (int i=0; i<=5; i++) {
myset.insert(i);
}
highest1=*myset.rbegin();
set<int>::iterator it=myset.begin();
while ( myCompKeyForSet(*it, highest1) ) it++;
cout << "\nhighest1 is " << highest1; // prints 5
highest2 = *myset.rbegin();
it=myset.begin();
while ( myCompValForSet(*it, highest2) ) it++;
cout << "\nhighest2 is " << highest2; // prints 5
return 0;
}
Output:
highest1 is 5
highest2 is 5
When we examine keycomp and valuecomp within set containers, these two terms are equivalent. Both functions will yield identical results for this type of container.