亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

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

C++ STL Set(集合)

C ++ set value_comp()函數(shù)的作用是:返回一個比較對象。這個函數(shù)用于比較兩個元素,以檢查第一個元素的鍵是否在第二個元素之前。用來比較value大小。

例如:-對于集合m,如果兩個元素e1(k1,d1)和e2(k2,d2)是value_type類型的對象,其中k1和k2是其key_type類型的鍵,而d1和d2是其data類型的數(shù)據(jù)value_type,則m value_comp(e1,e2)等效于m key_comp(k1,k2)。

語法

value_compare value_comp() const;

注意:存儲的對象定義了成員函數(shù):

bool-operator (value_type &left, value_type &right);

如果按排序順序,left的值在前面且不等于right的值,則返回true。

參數(shù)

沒有

返回值

它返回一個值比較函數(shù)對象。

復雜

不變。

迭代器有效性

沒有變化。

數(shù)據(jù)爭用

容器被訪問。

沒有包含元素的訪問:同時訪問集合的元素是安全的。

異常安全

如果引發(fā)異常,則容器中沒有任何更改。

實例1

讓我們看一下比較元素值的簡單示例:

#include <iostream>
#include <set>

using namespace std;

int main()
{
  set<int> c;
  set<int>::value_compare comp = c.value_comp();

  cout << "比較2和5(1為真,0為假): "<<comp(2, 5) << endl;
  cout << "比較8和5(1為真,0為假): "<<comp(8, 5) << endl;
}

輸出:

比較2和5(1為真,0為假): 1
比較8和5(1為真,0為假): 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 包含:";

  int highest=*myset.rbegin();
  set<int>::iterator it=myset.begin();
  do {
    cout << ' ' << *it;
  } while ( mycomp(*(++it),highest) );

  cout << '\n';

  return 0;
}

輸出:

myset 包含: 0 1 2 3 4

在上面的示例中,最高變量存儲myset集合的最后一個元素,并使用該集合的第一個元素(按排序順序)初始化迭代器。Do-while循環(huán)用于打印將在其中運行循環(huán)的元素,直到第一個鍵小于最后一個鍵為止(為此,它使用名為mycomp的key_comp()函數(shù))。

實例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)返回true值, "
           << "其中vc1是s1的函數(shù)對象。"
           << endl;
   }
   else   
   {
      cout << "vc1(2,3)返回false值, "
           << "其中vc1是s1的函數(shù)對象。"
           << 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)返回true值, "
           << "其中vc2是s2的函數(shù)對象。"
           << endl;
   }
   else   
   {
      cout << "vc2(2,3)返回false值, "
           << "其中vc2是s2的函數(shù)對象。"
           << endl;
   }
}

輸出:

vc1(2,3)返回true值,其中vc1是s1的函數(shù)對象。

vc2(2,3)返回false值,其中vc2是s2的函數(shù)對象。

實例4

讓我們看一個簡單的示例,以顯示key_comp()和value_comp()之間的區(qū)別:

#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()時,對于這樣的集合容器,這兩個詞是相同的。對于這兩種類型的函數(shù),它將返回相同的值。

C++ STL Set(集合)