C++ set key_comp()函数用于返回比较对象的副本,该副本由set集合用于比较键。
比较对象可用于比较集合中两个元素的键值。构造对象时会给出此比较对象,它可以是指向函数或函数对象的指针。在这两种情况下,这都采用两个相同类型的参数,如果第一个参数根据较弱的顺序在第二个参数之前,则返回true,否则返回false。
Key_compare key_comp() const;
operator bool ( const Key & _Left , const Key & _Right );
如果_Left在前面且排序顺序不等于_Right,则返回true。
它返回一个键比较函数对象。
让我们看一下比较键值的简单示例:
#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
在上面的示例中,comp(1,5)返回true,因为1小于5。comp(3,2)返回false,因为3不小于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()函数)。
让我们看一个简单的例子:
#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。
让我们看一个简单的例子:
#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。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
PPT设计进阶 · 从基础操作到高级创意 -〔李金宝(Bobbie)〕