C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Set key_comp()

C++ Set key_comp()

C++ Set key_comp()函数用于返回 Set 容器使用的比较对象的副本
比较对象可用于比较容器中两个元素的键值。构造对象时会给出此比较对象,它可以是指向函数或函数对象的指针。无论哪种情况,这都将采用两个相同类型的参数,如果第一个参数根据较弱的阶次在第二个参数之前,则返回true,否则返回false。
注意: 默认情况下,比较对象是一个less对象,它返回与运算符 <相同的对象。 < div>

语法

Key_compare key_comp() const;
注意: 存储的对象定义了成员函数:
operator bool ( const  Key &  _Left , const Key &  _Right );
如果_Left在排序顺序之前且不等于_Right,则返回true。

参数

返回值

它返回键比较函数对象。

复杂度

常量。

迭代器的有效性

没有更改。

数据竞争

已访问容器。
否可以访问包含的元素: 同时访问和修改元素是安全的。

异常安全性

如果引发异常,则容器中没有任何更改。

示例1

让我们看一下比较键值的简单示例:
#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 ; 
 }
输出:
Compare keys (1 is true and 0 is false):  1
Compare keys (1 is true and 0 is false):  0
在上面的示例中,因为1小于5,comp(1,5)返回true;由于3不小于2,comp(3,2)返回false。

示例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 contains:";
  highest=*myset.rbegin();
  set<int>::iterator it=myset.begin();
  do {
    cout << ' ' << *it;
  } while ( mycomp(*(++it),highest) );
  std::cout << '\n';
  return 0;
}
输出:
myset contains: 0 1 2 3 4
在上面的示例中,最高变量存储myset集的最后一个元素,并使用该集合的第一个元素(按排序顺序)初始化迭代器。 Do-while循环用于打印将在循环中运行的集合的元素,直到第一个键小于最后一个键为止(为此,它使用名为mycomp的key_comp()函数)。

示例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 ) 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;  
   }  
}  
输出:
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.
在上面的示例中,使用了两组,即m1和m2、 m1的键比较对象较小,而m2的键比较对象较大。因此,当我们比较(2,3)时,m1的kc1函数对象返回true,而m2的kc2函数对象返回false。

示例4

让我们看看一个简单的例子:
#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;
}
输出:
use function object kc to find less of (10, 4)...
kc(10, 4) == false, which means 10 > 4
在上面的示例中,set setobj的kc函数对象进行比较(10,4),如果为true,则返回10 <4;如果为true,则返回10> 4、

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4