English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ set value_comp() 사용법 및 예제

C++ STL Set(집합)

C ++ set value_comp()函数的作用是:返回一个比较对象。这个函数用于比较两个元素,以检查第一个元素的键是否在第二个元素之前。用来比较value大小。

例如:-对于集合m,如果两个元素e1(k1,d1)和e2(k2,d2)是value_type类型的对象,其中k1和k2是其key_type类型的键,而d1和d2是其data类型的数据value_type,则m value_comp(e1,e2)는 m key_comp(k1,k2)。

문법

value_compare value_comp() const;

주의: 저장된 객체는 구성원 함수를 정의했습니다:

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

정렬 순서에 따라 left의 값이 앞에 있으며 right의 값과 다르면 반환true

매개변수

없음

반환 값

그것은 값 비교 함수 객체를 반환합니다。

複잡

불변。

이터이터 유효성

변경 없음。

데이터 갈등

컨테이너가 접근됩니다。

요소에 대한 접근 없음: 집합의 요소에 동시에 접근하는 것은 안전합니다。

예외 안전

예외가 발생하면 컨테이너에 어떤 변경도 없습니다。

예제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)return true;여기서8이상5,comp(8,5)return 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

위 예제에서는 마지막 요소를 저장하는 변수 highest가 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)返回true值, "
           << "其中vc1은 s1의 함수 객체.
           << endl;
   }
   else   
   {
      cout << "vc1(2,3)返回false值, "
           << "其中vc1은 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)返回true值, "
           << "其中vc2은 s2의 함수 객체.
           << endl;
   }
   else   
   {
      cout << "vc2(2,3)返回false值, "
           << "其中vc2은 s2의 함수 객체.
           << endl;
   }
}

출력:

vc1(2,3)은 true 값을 반환합니다, 그리고 vc1은 s1의 함수 객체.
vc2(2,3)은 false 값을 반환합니다, 그리고 vc2은 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()를 비교할 때, 이러한 집합 컨테이너에서 이 두 단어는 동일합니다. 이 두 유형의 함수는 동일한 값을 반환합니다.

C++ STL Set(집합)