다음을 통해 공유


pair Structure

구조체 두 개체를 단일 개체로 취급 하는 기능을 제공 합니다.

template<class Type1, class Type2>
   struct pair 
   {
   typedef Type1 first_type;
   typedef Type2 second_type;
   Type1 first;
   Type2 second;
   pair( );
   pair(
      const Type1& __Val1, 
      const Type2& __Val2
   );
   template<class Other1, class Other2>
      pair(
         const pair<Other1, Other2>& _Right
      );
   template<class Other1, class Other2>
      pair(
         Other1&& _Val1, Other2&& _Val2
      );
   };

매개 변수

  • _Val1
    값의 첫 번째 요소를 초기화 하는 pair.

  • _Val2
    두 번째 요소에 초기화 된 값 pair.

  • _Right
    쌍 다른 쌍의 요소를 초기화 하는 데 사용할 값입니다.

반환 값

기본 형식의 쌍의 첫 번째 요소는 첫 번째 (기본) 생성자 초기화 Type1 하 고 두 번째 요소에 기본 형식의 Type2.

첫 번째 요소에 한 쌍의 두 번째 생성자 초기화 _Val1 및 두 번째에 _Val2.

첫 번째 요소에 한 쌍의 세 번째 (템플릿) 생성자 초기화 _Right. 첫 번째 및 두 번째에 _Right.second.

첫 번째 요소에 한 쌍의 네 번째 생성자 초기화 _Val1 및 두 번째에 _Val2 를 사용 하 여 Rvalue 참조 선언 자: & &.

설명

템플릿 구조체 형식의 개체 쌍을 저장 Type1Type2, 각각.형식 first_type 템플릿 매개 변수 같은 Type1 및 형식 second_type 템플릿 매개 변수 같은 Type2.Type1Type2 각 단일 인수 생성자를 기본 생성자, 소멸자를 제공 해야 합니다.형식의 모든 멤버 pair 형식으로 선언 된 때문에 공용 되는 struct 대신으로 클래스.결합형 컨테이너 클래스를 요소로 하 고 두 값을 반환 하는 함수는 반환 형식 쌍에 대 한 가장 일반적인 용도 클래스에 매핑할multimap 클래스 키와 각 요소와 연관 된 값 형식이 있습니다.후자 쌍 결합형 컨테이너에 대 한 요구 사항을 충족 하 고 폼의 값 형식이 pair<constkey_type, mapped_type>.

예제

// utility_pair.cpp
// compile with: /EHsc
#include <utility>
#include <map>
#include <iomanip>
#include <iostream>

int main( )
{
   using namespace std;

   // Using the constructor to declare and initialize a pair
   pair <int, double> p1 ( 10, 1.1e-2 );

   // Compare using the helper function to declare and initialize a pair
   pair <int, double> p2;
   p2 = make_pair ( 10, 2.22e-1 );

   // Making a copy of a pair
   pair <int, double> p3 ( p1 );

   cout.precision ( 3 );
   cout << "The pair p1 is: ( " << p1.first << ", " 
        << p1.second << " )." << endl;
   cout << "The pair p2 is: ( " << p2.first << ", " 
        << p2.second << " )." << endl;
   cout << "The pair p3 is: ( " << p3.first << ", " 
        << p3.second << " )." << endl;

   // Using a pair for a map element
   map <int, int> m1;
   map <int, int>::iterator m1_Iter;

   typedef pair <int, int> Map_Int_Pair;

   m1.insert ( Map_Int_Pair ( 1, 10 ) );
   m1.insert ( Map_Int_Pair ( 2, 20 ) );
   m1.insert ( Map_Int_Pair ( 3, 30 ) );

   cout << "The element pairs of the map m1 are:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " ( " << m1_Iter -> first << ", "
           << m1_Iter -> second << " )";
   cout   << "." << endl;

   // Using pair as a return type for a function
   pair< map<int,int>::iterator, bool > pr1, pr2;
   pr1 = m1.insert ( Map_Int_Pair ( 4, 40 ) );
   pr2 = m1.insert ( Map_Int_Pair (1, 10 ) );

   if( pr1.second == true )
   {
      cout << "The element (4,40) was inserted successfully in m1."
           << endl;
   }
   else   
   {
      cout << "The element with a key value of\n"
           << " ( (pr1.first) -> first ) = " << ( pr1.first ) -> first 
           << " is already in m1,\n so the insertion failed." << endl;
   }

   if( pr2.second == true )
   {
      cout << "The element (1,10) was inserted successfully in m1."
           << endl;
   }
   else   
   {
      cout << "The element with a key value of\n"
           << " ( (pr2.first) -> first ) = " << ( pr2.first ) -> first 
           << " is already in m1,\n so the insertion failed." << endl;
   }
}
  
  
  
  
  
  

요구 사항

헤더: <utility>

네임 스페이스: std

참고 항목

참조

Pair Logical Operator

표준 C++ 라이브러리에서 스레드로부터의 안전성