TheDeveloperBlog.com

Home | Contact Us

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

C++ set key_comp() Function

C++ set key_comp() Function with tutorial for beginners and professionals with examples on constructor, destructor, operator=(), begin(), cbegin(), cend(), end(), crbegin(), empty(), max_size(), clear(), emplace_hint(), key_comp(), swap() etc.

<< Back to CPP

C++ set key_comp()

C++ set key_comp() function is used to return a copy of the comparison object which is used by the set 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 );

Returns true if _Left precedes and is not equal to _Right in the sort order.

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 () 
 { 
  set < int >  m ; 
  set < 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 ; 
 }

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 ()
{
  set<int> myset;
  int highest;

  set<int>::key_compare mycomp = myset.key_comp();

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

  cout << "myset contains:";

  highest=*myset.rbegin();
  set<int>::iterator it=myset.begin();
  do {
    cout << ' ' << *it;
  } while ( mycomp(*(++it),highest) );

  std::cout << '\n';

  return 0;
}

Output:

myset contains: 0 1 2 3 4

In the above example, highest variable stores the last element of the myset set and iterator initialized with first element of the set (in sorted order). Do-while loop is used to print the element of the set 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;  
  
   set <int, less<int> > s1;  
   set<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;  
   }  
  
   set <int, greater<int> > s2;  
   set<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;  
   }  
}  

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 sets 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 set<int> setObj ;

int main(){

	//default constructor
	setObj c1 ;
	
    setObj::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 set setobj 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.






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