English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ set key_comp()The 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 the key values of two elements in the container. This comparison object is given at the time of constructing the object and can be a pointer to a function or a function object. In both cases, it accepts two parameters of the same type and returns true if the first parameter is before the second parameter, otherwise returns false.
Key_compare key_comp() const;
operator bool ( const Key & _Left , const Key & _Right );
Returns true if _Left is in front and the sorting order does not equal _Right.
None
It returns the key comparison function object.
Unchanged.
No changes.
The container is accessed.
No access to any contained elements: accessing and modifying elements is safe at the same time.
If an exception is thrown, there are no changes in the container.
Let's see a simple example of comparing key values:
#include <iostream> #include <set> using namespace std; int main () { set < int > m ; set < int > :: key_compare comp = m . key_comp () ; cout << "compare key(1is true, 0 is false): << "<< comp ( 1 , 5 ) << endl; cout << "compare key(1is true, 0 is false): << "<< comp ( 3 , 2 ) << endl; }
Saída:
compare key(1is true, 0 is false): 1 compare key(1is true, 0 is false): 0
In the above example, comp(1,5)returns true because1less5。comp(3,2)returns false because3greater2.
让我们看一个简单的实例:
#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; }
Saída:
myset包含: 0 1 2 3 4
在上面的示例中,最高变量存储myset集合的最后一个元素,并使用该集合的第一个元素(按排序顺序)初始化迭代器。Do-while循环用于打印将在其中运行循环的元素,直到第一个键小于最后一个键为止(为此,它使用名为mycomp的key_comp()函数)。
让我们看一个简单的实例:
#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; } }
Saída:
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。
让我们看一个简单的实例:
#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, ou seja10 <4" << endl; else cout << "kc(10,4) == false, ou seja10 > 4" << endl; return 0; }
Saída:
Usar o objeto de função kc para encontrar comparação(10,4)... kc(10,4) == false, ou seja10 > 4
No exemplo acima, o objeto de função kc do set setobj realiza compare(10,4),se for true, então retorna10 <4Se não for true, então retorna10> 4.