Multiset Value Comp Function

C++ Multiset value_comp function returns a comparison object . This function is used to compare two elements to check whether the key of the first one goes before the second.

It takes two arguments of the same type and returns true if the first argument precedes the second argument according to the narrower weak order, otherwise returns false .

E.g. : - For a multiset m, if two elements e1(k1, d1) and e2( k2, d2) are objects of type valuetype, where k1 and k2 are their keys of type keytype and d1 and d2 are their data, then m valuecomp( e1 , e2 ) is equivalent to m keycomp(k1, k2).

Syntax

Example

value_compare value_comp() const;

Note: A stored object defines a member function:

Example

bool-operator (value_type &left, value_type &right);

It returns true if the value of the left key precedes and does not equal the value of the key from right in the sort order.

Parameter

Return value

It returns a value comparison function object.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

No contained elements are accessed: Concurrently accessing the elements of a multiset is safe.

Exception Safety

If an exception is thrown, there are no changes in the container.

Example 1

Let's see the simple example to compare values of elements:

Example

#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:

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 above example, comp(2, 5) returns true because 2 is less than 5. And comp(8, 5) returns false because 8 is not less than 5.

Example 2

Let's see a simple example:

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:

Output

mymultiset contains: 0 1 2 3 4

In the above example, highest variable stores the last element of the mymultiset multiset and iterator initialized with first element of the multiset (in sorted order). Do-while loop is used to print the element of the multiset where the loop will run until first key is less than last key (for this it is using key_comp function named as mycomp).

Example 3

Let's see a simple example:

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:

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 see a simple example to show the difference between keycomp and valuecomp:

Example

#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:

Output

highest1 is 5
highest2 is 5

In the above example, when we compare keycomp and valuecomp then for such multiset containers these two words are the same. For both type of functions it will return the same value.

Input Required

This code uses input(). Please provide values below: