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

C++ Set value_comp()

C++ Set value_comp()

C++ Set value_comp()函数返回比较对象。此函数用于比较两个元素,以检查第一个元素的键是否位于第二个元素之前。
它接受两个相同类型的参数,并且如果第一个参数位于第二个参数之前,则返回true
例如 。:-对于集合m,如果两个元素e1(k1,d1)和e2(k2,d2)是对象类型为value_type的数据,其中k1和k2是其key_type类型的密钥,d1和d2是其setped_type类型的数据,则m value_comp(e1,e2)等于m key_comp(k1,k2)。

语法

value_compare value_comp() const;
注意: 存储的对象定义了一个成员函数:
bool-operator (value_type &left, value_type &right);
如果左键的值位于排序顺序的前面,并且不等于右键的值,则返回 true 。

参数

返回值

它返回一个值比较函数对象。

复杂性

恒定。

迭代器有效性

没有变化。

数据竞争
已访问容器。
未访问任何包含的元素: 同时访问集合的元素是安全的。

异常安全

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

示例1

让我们看一下比较元素值的简单示例:
#include <iostream>
#include <set>
using namespace std;
int main()
{
  set<int> c;
  set<int>::value_compare comp = c.value_comp();
  cout << "Compare 2 to 5 (1 is true and 0 is false): "<<comp(2, 5) << endl;
  cout << "Compare 8 to 5 (1 is true and 0 is false): "<<comp(8, 5) << endl;
}
输出:
Compare 2 to 5 (1 is true and 0 is false): 1
Compare 8 to 5 (1 is true and 0 is false): 0
在上面的示例中,由于2小于5,comp(2,5)返回true;由于8不小于5,comp(8,5)返回false。

示例2

让我们看一个简单的示例:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  set<int> myset;
  set<int>::value_compare mycomp = myset.value_comp();
  for (int i=0; i<=5; i++) myset.insert(i);
  cout << "myset contains:";
  int highest=*myset.rbegin();
  set<int>::iterator it=myset.begin();
  do {
    cout << ' ' << *it;
  } while ( mycomp(*(++it),highest) );
  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> >::value_compare vc1 = s1.value_comp( );
   bool result1 = vc1( 2, 3 );
   if( result1 == true )   
   {
      cout << "vc1( 2,3 ) returns value of true, "
           << "where vc1 is the function object of s1."
           << endl;
   }
   else   
   {
      cout << "vc1( 2,3 ) returns value of false, "
           << "where vc1 is the function object of s1."
           << endl;
   }
   set <int, greater<int> > s2;
   set<int, greater<int> >::value_compare vc2 = s2.value_comp( );
   bool result2 = vc2( 2, 3 );
   if( result2 == true )   
   {
      cout << "vc2( 2,3 ) returns value of true, "
           << "where vc2 is the function object of s2."
           << endl;
   }
   else   
   {
      cout << "vc2( 2,3 ) returns value of false, "
           << "where vc2 is the function object of s2."
           << endl;
   }
}
输出:
vc1( 2,3 ) returns value of true, where vc1 is the function object of s1.
vc2( 2,3 ) returns value of false, where vc2 is the function object of s2.

示例4

让我们看一个简单的示例,以显示key_comp()和value_comp()的区别:
#include <set>
#include <iostream>
#include<map>
using namespace std;
int main(){
set<int> myset;
int highest1, highest2;
set<int>::key_compare   myCompKeyForSet = myset.key_comp();
set<int>::value_compare myCompValForSet = myset.value_comp();
for (int i=0; i<=5; i++) {
  myset.insert(i);
}
highest1=*myset.rbegin();
set<int>::iterator it=myset.begin();
while ( myCompKeyForSet(*it, highest1) ) it++;
cout << "\nhighest1 is " << highest1;  // prints 5
highest2 = *myset.rbegin();
it=myset.begin();
while ( myCompValForSet(*it, highest2) ) it++;
cout << "\nhighest2 is " << highest2;   // prints 5
return 0;
}
输出:
highest1 is 5
highest2 is 5
在上面的示例中,当我们比较key_comp()和value_comp()时,对于这样的集合容器,这两个词是相同的。对于这两种函数,它将返回相同的值。

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