亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

C++ set key_comp() 使用方法及示例

C++ STL Set(集合)

C ++ set key_comp()函數(shù)用于返回比較對象的副本,該對象由set容器用于比較鍵。

比較對象可用于比較容器中兩個元素的鍵值。這個比較對象是在構(gòu)造對象時給出的,它可以是一個指向函數(shù)的指針,也可以是一個函數(shù)對象。在這兩種情況下,它都接受相同類型的兩個參數(shù),如果第一個參數(shù)在第二個參數(shù)之前,則返回true,否則返回false。

注意:默認情況下,比較對象是less對象,它返回的值與運算符<相同。

語法

Key_compare key_comp() const;

注意:存儲的對象定義了成員函數(shù):

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

如果_Left在前面且排序順序不等于_Right,則返回true。

參數(shù)

沒有

返回值

它返回鍵比較功能對象。

復(fù)雜

不變。

迭代器有效性

沒有變化。

數(shù)據(jù)爭用

容器被訪問。

不能訪問任何包含的元素:同時訪問和修改元素是安全的。

異常安全

如果引發(fā)異常,則容器中沒有任何更改。

實例1

讓我們看一下比較鍵值的簡單示例:

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

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

  cout  <<"比較鍵(1為真,0為假):  "<<  comp ( 1 ,  5 )  <<endl ; 
  cout  <<"比較鍵(1為真,0為假):  "<<  comp ( 3 ,  2 )  <<endl ; 
 }

輸出:

比較鍵(1為真,0為假):  1
比較鍵(1為真,0為假):  0

在上面的示例中,comp(1,5)返回true,因為1小于5。comp(3,2)返回false,因為3大于2。

實例2

讓我們看一個簡單的實例:

#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包含:";

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

  std::cout << '\n';

  return 0;
}

輸出:

myset包含: 0 1 2 3 4

在上面的示例中,最高變量存儲myset集合的最后一個元素,并使用該集合的第一個元素(按排序順序)初始化迭代器。Do-while循環(huán)用于打印將在其中運行循環(huán)的元素,直到第一個鍵小于最后一個鍵為止(為此,它使用名為mycomp的key_comp()函數(shù))。

實例3

讓我們看一個簡單的實例:

#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)返回true值"  
           << "其中kc1是s1的函數(shù)對象."  
           << endl;  
   }  
   else     
   {  
      cout << "kc1(2,3)返回false值 "  
           << "其中kc1是s1的函數(shù)對象。"  
           << 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)返回true值, "  
           << "其中kc2是s2的函數(shù)對象。"  
           << endl;  
   }  
   else     
   {  
      cout << "kc2(2,3)返回false值, "  
           << "其中kc2是s2的函數(shù)對象。"  
           << endl;  
   }  
}

輸出:

kc1(2,3)返回值true,其中kc1是s1的函數(shù)對象。
kc2(2,3)返回false值,其中kc2是s2的函數(shù)對象。

在上面的示例中,使用了兩個集合,即m1和m2。m1的鍵比較對象較小,而m2的鍵比較對象較大。因此,當我們compare(2,3)時,m1的kc1函數(shù)對象返回true,而m2的kc2函數(shù)對象返回false。

實例4

讓我們看一個簡單的實例:

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

using namespace std;

typedef set<int> setObj ;

int main(){

	//默認構(gòu)造函數(shù)
	setObj c1 ;
	
    setObj::key_compare kc = c1.key_comp() ;
	cout << "使用函數(shù)對象kc查找比較(10,4)..." 
		<< endl ;
		
	if (kc(10, 4) == true)
		cout << "kc(10,4)== true,即10 <4" << endl ;
	else
		cout << "kc(10,4) == false,即10 > 4" << endl ;
		
return 0;
}

輸出:

使用函數(shù)對象kc查找比較(10,4)...
kc(10,4) == false,即10 > 4

在上面的示例中,set setobj的kc函數(shù)對象進行compare(10,4),如果為true,則返回10 <4;如果不為true,則返回10> 4。

C++ STL Set(集合)