C++ multiset key_comp()
C++ multiset key_comp()
C++ multiset key_comp()函数用于返回 multiset 容器使用的比较对象的副本
比较对象可用于比较容器中两个元素的键值。构造对象时会给出此比较对象,它可以是指向函数或函数对象的指针。在这两种情况下,这都将采用两个相同类型的参数,如果第一个参数根据较弱的阶次在第二个参数之前,则返回true,否则返回false。
注意: 默认情况下,比较对象是一个less对象,它返回与运算符相同的对象。
语法
Key_compare key_comp() const;
注意: 存储的对象定义了成员函数:
operator bool ( const Key & _Left , const Key & _Right );
参数
无
返回值
它返回键比较函数对象。
复杂度
常量。
迭代器有效性
没有变化。
数据竞争
已访问容器。
未访问任何包含的元素: 同时访问和修改元素是安全的。
异常安全性
如果引发异常,则容器中没有任何变化。
示例1
让我们看看比较键值的简单示例:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset < int > m ;
multiset < 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 ;
return 0;
}
输出:
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、
示例2
让我们看一个简单的示例:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset;
int highest;
multiset<int>::key_compare mycomp = mymultiset.key_comp();
for (int i=0; i<=5; i++) mymultiset.insert(i);
cout << "mymultiset contains:";
highest=*mymultiset.rbegin();
multiset<int>::iterator it=mymultiset.begin();
do {
cout << ' ' << *it;
} while ( mycomp(*(++it),highest) );
cout << '\n';
return 0;
}
输出:
mymultiset contains: 0 1 2 3 4
在上面的示例中,最高变量存储mymultiset multiset 的最后一个元素,并使用该 multiset 的第一个元素(按排序顺序)初始化迭代器。 Do-while循环用于打印将在其中运行循环的 multiset 元素,直到第一个键小于最后一个键为止(为此,它使用名为mycomp的key_comp()函数)。
示例3
让我们看一个简单的示例:
#include <set>
#include <iostream>
int main( )
{
using namespace std;
multiset <int, less<int> > s1;
multiset<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;
}
multiset <int, greater<int> > s2;
multiset<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;
}
return 0;
}
输出:
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.
在上面的示例中,使用了两个 multiset ,即m1和m2、 m1的键比较对象较小,而m2的键比较对象较大。因此,当我们比较(2,3)时,m1的kc1函数对象返回true,而m2的kc2函数对象返回false。
示例4
让我们看看一个简单的例子:
#include <set>
#include <iostream>
#include <string>
using namespace std;
typedef multiset<int> multisetObj ;
int main(){
//default constructor
multisetObj c1 ;
multisetObj::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
在上面的示例中, multiset multisetobj的kc函数对象比较(10,4)如果为true,则将返回10 < 4;如果为非true,则将返回10 > 4、