TheDeveloperBlog.com

Home | Contact Us

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

C++ multiset key_comp() function

C++ multiset key_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 key_comp()

C++ Multiset key_comp() function is used to return a copy of the comparison object which is used by the multiset container to compare keys.

The comparison object can be used to compare key values of two elements in a container. This comparison object is given when constructing the object and it can be a pointer to a function or a function object. In either case, this takes two arguments of the same type, returning true if the first argument is before the second argument according to the narrower weak order, otherwise returns false.

Note: By default, comparison object is a less object, which returns the same as operator <.

Syntax

Key_compare key_comp() const;

Note: A stored object defines member functions:

operator bool ( const  Key &  _Left , const Key &  _Right );

Parameter

None

Return value

It returns a key comparison function object.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

No contained elements are accessed: Concurrently accessing and modifying the elements 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 key values:

#include <iostream>
#include <set>
 
 using namespace std;

 int  main () 
 { 
  multiset < int >  m ; 
  multiset < int > :: key_compare  comp  =  m . key_comp () ; 

  cout  <<"Compare keys (1 is true and 0 is false):  "<<  comp ( 1 ,  5 )  <<endl ; 
  cout  <<"Compare keys (1 is true and 0 is false):  "<<  comp ( 3 ,  2 )  <<endl ; 
return 0;
 }

Output:

Compare keys (1 is true and 0 is false):  1
Compare keys (1 is true and 0 is false):  0

In the above example, comp(1, 5) returns true because 1 is less than 5 and comp(3, 2) returns false because 3 is not less than 2.

Example 2

Let's see a simple example:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  multiset<int> mymultiset;
  int highest;

  multiset<int>::key_compare mycomp = mymultiset.key_comp();

  for (int i=0; i<=5; i++) mymultiset.insert(i);

  cout << "mymultiset contains:";

  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> >::key_compare kc1 = s1.key_comp( ) ;  
   bool result1 = kc1( 2, 3 ) ;  
   if( result1 == true )     
   {  
      cout << "kc1( 2,3 ) returns value of true, "  
           << "where kc1 is the function object of s1."  
           << endl;  
   }  
   else     
   {  
      cout << "kc1( 2,3 ) returns value of false "  
           << "where kc1 is the function object of s1."  
           << endl;  
   }  
  
   multiset <int, greater<int> > s2;  
   multiset<int, greater<int> >::key_compare kc2 = s2.key_comp( ) ;  
   bool result2 = kc2( 2, 3 ) ;  
   if(result2 == true)     
   {  
      cout << "kc2( 2,3 ) returns value of true, "  
           << "where kc2 is the function object of s2."  
           << endl;  
   }  
   else     
   {  
      cout << "kc2( 2,3 ) returns value of false, "  
           << "where kc2 is the function object of s2."  
           << endl;  
   }  
   return 0;
}  

Output:

kc1( 2,3 ) returns value of true, where kc1 is the function object of s1.
kc2( 2,3 ) returns value of false, where kc2 is the function object of s2.

In the above example, there are two multisets used i.e. m1 and m2. The key comparison object of m1 is less and key comparison object of m2 is greater. Therefore, when we compare (2, 3) then kc1 function object of m1 returns true and kc2 function object of m2 returns false.

Example 4

Let's see a simple example:

#include <set>
#include <iostream>
#include <string>

using namespace std;

typedef multiset<int> multisetObj ;

int main(){

	//default constructor
	multisetObj c1 ;
	
    multisetObj::key_compare kc = c1.key_comp() ;
	cout << "use function object kc to find less of (10, 4)..." 
		<< endl ;
		
	if (kc(10, 4) == true)
		cout << "kc(10, 4) == true, which means 10 < 4" << endl ;
	else
		cout << "kc(10, 4) == false, which means 10 > 4" << endl ;
		
return 0;
}

Output:

use function object kc to find less of (10, 4)...
kc(10, 4) == false, which means 10 > 4

In the above example, kc function object of multiset multisetobj compares (10, 4) if it is true, then it will return 10 < 4 and if it is not true, then it will return 10 > 4.

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