English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ 教程 de base

C++ Contrôle de flux

C++ Fonction

C++ Tableau & chaîne

C++ Structure de données

C++ Classe & objet

C++ Pointeur

C++ Héritage

C++ STL tutoriel

C++ 参考手册

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

C++ Set (ensemble) STL

C ++ set key_comp()函数用于返回比较对象的副本,该对象由set容器用于比较键。

比较对象可用于比较容器中两个元素的键值。这个比较对象是在构造对象时给出的,它可以是一个指向函数的指针,也可以是一个函数对象。在这两种情况下,它都接受相同类型的两个参数,如果第一个参数在第二个参数之前,则返回true,否则返回false。

注意:默认情况下,比较对象是less对象,它返回的值与运算符<相同。

语法

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 << "比较键(1为真,0为假): "<< comp( 1 ,  5 ) <<endl; 
  cout << "比较键(1为真,0为假): "<< comp( 3 ,  2 ) <<endl; 
 }

Sortie :

比较键(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;
}

Sortie :

myset包含: 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)返回true值"  
           << "其中kc1是s1的函数对象."  
           << endl;  
   }  
   else     
   {  
      cout << "kc1(2,3)返回false值 "  
           << "其中kc1是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)返回true值, "  
           << "其中kc2是s2的函数对象。"  
           << endl;  
   }  
   else     
   {  
      cout << "kc2(2,3)返回false值, "  
           << "其中kc2是s2的函数对象。"  
           << endl;  
   }  
}

Sortie :

kc1(2,3)返回值true,其中kc1是s1的函数对象。
kc2(2,3)返回false值,其中kc2是s2的函数对象。

在上面的示例中,使用了两个集合,即m1和m2。m1的键比较对象较小,而m2的键比较对象较大。因此,当我们compare(2,3)时,m1的kc1函数对象返回true,而m2的kc2函数对象返回false。

实例4

让我们看一个简单的实例:

#include <set>
#include <iostream>
#include <string>
using namespace std;
typedef set<int> setObj;
int main(){}
	//默认构造函数
	setObj c1 ;
    setObj::key_compare kc = c1.key_comp();
	cout << "使用函数对象kc查找比较(10,4)... 
		<< endl ;
	if (kc(10, 4) == true)
		cout << "kc(10,4)== true, c'est-à-dire10 <4" << endl ;
	else
		cout << "kc(10,4) == false, c'est-à-dire10 > 4" << endl ;
return 0;
}

Sortie :

Recherche de comparaison kc en utilisant l'objet de fonction kc10,4)...
kc(10,4) == false, c'est-à-dire10 > 4

Dans l'exemple ci-dessus, l'objet de fonction kc du set setobj effectue compare (10,4),si c'est true, retourne10 <4Si ce n'est pas true, retourne10> 4.

C++ Set (ensemble) STL