次の方法で共有


pair 構造体

単一のオブジェクトとして 2 個のオブジェクトを処理する機能を提供する構造体。

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の 2 番目の要素を初期化する値。

  • _Right
    別の pair 要素を初期化するために使用される値のペア。

戻り値

最初の (既定の) コンストラクターが型 Type2の既定の型 Type1 の既定にタプルの最初の要素と 2 番目の要素を初期化します。

2 番目のコンストラクターは _Val1 と second _Val2にタプルの最初の要素を初期化します。

3 番目の (テンプレート) コンストラクターは _Rightにタプルの最初の要素を初期化します。_Rightへの最初 と second。第 2

4 番目のコンストラクターは 右辺値参照宣言子: &&を使用して _Val1 と second _Val2 にタプルの最初の要素を初期化します。

解説

テンプレートの構造体は型 Type1Type2オブジェクトのペアがそれぞれ格納します。 first_type 型は、テンプレート パラメーター Type1 と型 second_type がテンプレート パラメーター Type2とほぼ同じです。 Type1Type2 は各必要既定コンストラクター、単一引数のコンストラクターとデストラクターのみを指定します。 pair 型のすべてのメンバーは、型が クラスではなく struct として宣言されているので、パブリックです。 ペアの 2 種類の一般的な用途は、値 2 を返すと各要素に関連付けられるキーと値型の両方を持つ連想コンテナー クラス マップ クラスmultimap クラス の要素として、関数の戻り値の型。 後者は、ペアの連想コンテナーの要件を満たす、フォーム pair<const key_typemapped_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;
   }
}
  

必要条件

ヘッダー: <ユーティリティ>

名前空間: std

参照

関連項目

2項論理演算子

C++ 標準ライブラリ内のスレッド セーフ