TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ multiset value_comp() function

C++ multiset value_comp() Function with Examples on tutorial for beginners and professionals with examples on multiset, begin(), end(), multiset constructor, ~multiset destructor, operator=, rbegin(), rend() etc.

<< Back to CPP

C++ multiset value_comp()

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 value_type, where k1 and k2 are their keys of type key_type and d1 and d2 are their data, then m value_comp( e1 , e2 ) is equivalent to 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 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

None

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:

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

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

#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 see a simple example to show the difference between key_comp() and value_comp():

#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

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

Next TopicC++ multiset




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf