C++ 標準テンプレート ライブラリ (STL) でのペアリング

ペアは、データ型が異なる 2 つの値を結合するために使用されます。ペアは、2 つの異種オブジェクトを 1 つのユニットとして保存する方法を提供します。基本的にタプルを保存したい場合に使用されます。ペアコンテナは、次のように定義された単純なコンテナです。 2 つのデータ要素またはオブジェクトで構成されるヘッダー。

  • 最初の要素は「first」として参照され、2 番目の要素は「next」として参照され、順序は固定されています (first、sec)。
  • ペアの割り当て、コピー、比較が可能です。に割り当てられたオブジェクトの配列 地図 または hash_map はデフォルトで「pair」タイプであり、すべての「最初の」要素は「2 番目」の値オブジェクトに関連付けられた一意のキーです。
  • 要素にアクセスするには、変数名、ドット演算子、キーワード first または Second を使用します。

構文:

pair Pair_name 

CPP




// CPP program to illustrate Pair in STL> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> > // defining a pair> > pair <> int> ,> char> >ペア 1;>>' ;> > cout < < PAIR1.first < <> ' '> ;> > cout < < PAIR1.second < < endl;> > return> 0;> }>

出力

100 G 

ペアの初期化: ペアを初期化することもできます。

構文:

pair Pair_name (value1, value2) ; 

ペアを初期化するさまざまな方法:

pair g1; //default pair g2(1, 'a'); //initialized, different data type pair g3(1, 10); //initialized, same data type pair g4(g3); //copy of g3 

ペアを初期化するもう 1 つの方法は、make_pair() 関数を使用することです。

g2 = make_pair(1, 'a'); 

ペアを宣言するための別の有効な構文は次のとおりです。

g2 = {1, 'a'}; 

CPP




// CPP program to illustrate> // Initializing of pair STL> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> > // defining a pair> > pairdouble>PAIR2('GeeksForGeeks', 1.23); コート < < PAIR2.first < < ' '; cout < < PAIR2.second < < endl; return 0; }>

出力

GeeksForGeeks 1.23 

注記: 初期化されていない場合、ペアの最初の値が自動的に初期化されます。

C++




// CPP program to illustrate> // auto-initializing of pair STL> #include> #include> using> namespace> std;> int> main()> {> > pair <> int> ,> double> >ペア 1;>>'

出力:

00 

メンバー関数

1) make_pair() : このテンプレート関数を使用すると、型を明示的に記述せずに値のペアを作成できます。
構文:

Pair_name = make_pair (value1,value2); 

CPP




// CPP Program to demonstrate make_pair()> // function in pair> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> > pair <> int> ,> char> >ペア 1;>>' ペア3; PAIR1.first = 100; PAIR1.秒 = 'G'; PAIR3 = make_pair('GeeksForGeeks がベストです', 4.56); コート < < PAIR1.first < < ' '; cout < < PAIR1.second < < endl; cout < < PAIR2.first < < ' '; cout < < PAIR2.second < < endl; cout < < PAIR3.first < < ' '; cout < < PAIR3.second < < endl; return 0; }>

出力

100 G GeeksForGeeks 1.23 GeeksForGeeks is Best 4.56 

2) 交換: この関数は、1 つのペア オブジェクトの内容を別のペア オブジェクトの内容と交換します。ペアは同じタイプである必要があります。
構文:

pair1.swap(pair2) ; 

同じタイプのペア 1 とペア 2 などの 2 つの指定されたペアの場合、swap 関数は、pair1.first と par2.first を交換し、pair1.second と par2.second を交換します。

CPP




// CPP Program to demonstrate swap()> // function in pair> #include> #include> using> namespace> std;> // Driver Code> int> main()> {> > pair <> char> ,> int> >ペア1 = make_pair(>> 'A'> , 1);> > pair <> char> ,> int> >ペア2 = make_pair(>> 'B'> , 2);> > cout < <> 'Before swapping: '> ;> > cout < <> 'Contents of pair1 = '> < < pair1.first < <> ' '> > < < pair1.second;> > cout < <> 'Contents of pair2 = '> < < pair2.first < <> ' '> > < < pair2.second;> > pair1.swap(pair2);> > cout < <> ' After swapping: '> ;> > cout < <> 'Contents of pair1 = '> < < pair1.first < <> ' '> > < < pair1.second;> > cout < <> 'Contents of pair2 = '> < < pair2.first < <> ' '> > < < pair2.second;> > return> 0;> }>

出力

Before swapping: Contents of pair1 = A 1Contents of pair2 = B 2 After swapping: Contents of pair1 = B 2Contents of pair2 = A 1 

3) タイ(): この関数は以下と同じように動作します。 タプル 。これは、その引数への左辺値参照のタプルを作成します。つまり、タプル (またはここではペア) の値を個別の変数に解凍します。タプルと同様に、無視ありとなしの 2 つのタイのバリエーションもここにあります。 ignore キーワードは、特定のタプル要素が解凍されるのを無視します。
ただし、タプルには複数の引数を指定できますが、ペアには 2 つの引数しかありません。したがって、ペアのペアの場合、アンパックを明示的に処理する必要があります。
構文:

tie(int &, int &) = pair1; 

CPP




// CPP code to illustrate tie() in Pair> #include> using> namespace> std;> // Driver Code> int> main()> {> > pair <> int> ,> int> >ペア1 = { 1, 2 };>> > int> a, b;> > tie(a, b) = pair1;> > cout < < a < <> ' '> < < b < <> ' '> ;> > pair <> int> ,> int> >ペア2 = { 3, 4 };>> > tie(a, ignore) = pair2;> > // prints old value of b> > cout < < a < <> ' '> < < b < <> ' '> ;> > // Illustrating pair of pairs> > pair <> int> , pair <> int> ,> char> >> ペア 3 = { 3, { 4,>> 'a'> } };> > int> x, y;> > char> z;> > // tie(x,y,z) = pair3; Gives compilation error> > // tie(x, tie(y,z)) = pair3; Gives compilation error> > // Each pair needs to be explicitly handled> > tie(x,ignore) = pair3;> > tie(y, z) = pair3.second;> > cout < < x < <> ' '> < < y < <> ' '> < < z < <> ' '> ;> }> // contributed by sarthak_eddy.>

出力

1 2 3 2 3 4 a 

ペアの関数を示すコード:

CPP




// CPP program to illustrate pair in STL> #include> #include> #include> using> namespace> std;> int> main()> {> > pairint>g1; ペア整数> g2('クイズ', 3); ペア整数> g3(g2); ペア g4(5, 10); g1 = make_pair(string('Geeks'), 1); g2.first = '.com'; g2.秒 = 2; コート < < 'This is pair g' < < g1.second < < ' with ' < < 'value ' < < g1.first < < '.' < < endl < < endl; cout < < 'This is pair g' < < g3.second < < ' with value ' < < g3.first < < 'This pair was initialized as a copy of ' < < 'pair g2' < < endl < < endl; cout < < 'This is pair g' < < g2.second < < ' with value ' < < g2.first < < ' The values of this pair were' < < ' changed after initialization.' < < endl < < endl; cout < < 'This is pair g4 with values ' < < g4.first < < ' and ' < < g4.second < < ' made for showing addition. The ' < < 'sum of the values in this pair is ' < < g4.first + g4.second < < '.' < < endl < < endl; cout < < 'We can concatenate the values of' < < ' the pairs g1, g2 and g3 : ' < < g1.first + g3.first + g2.first < < endl < < endl; cout < < 'We can also swap pairs ' < < '(but type of pairs should be same) : ' < < endl; cout < < 'Before swapping, ' < < 'g1 has ' < < g1.first < < ' and g2 has ' < < g2.first < < endl; swap(g1, g2); cout < < 'After swapping, ' < < 'g1 has ' < < g1.first < < ' and g2 has ' < < g2.first; return 0; }>

出力

This is pair g1 with value Geeks. This is pair g3 with value QuizThis pair was initialized as a copy of pair g2 This is pair g2 with value .com The values of this pair were changed after initialization. This is pair g4 with values 5 and 10 made for showing addition. The sum of the values in this pair is 15. We can concatenate the values of the pairs g1, g2 and g3 : GeeksQuiz.com We can also swap pairs (but type of pairs should be same) : Before swapping, g1 has Geeks and g2 has .com After swapping, g1 has .com and g2 has Geeks 

時間計算量: ○(1)。

補助スペース: ○(1)。

ペアの演算子(=、==、!=、>=、 <=)

ペアの演算子も使用できます。

1) 等しい(=)を使用します: ペア オブジェクトに新しいオブジェクトを割り当てます。構文:

pair& operator= (const pair& pr); 

これにより、ペア オブジェクトの新しいコンテンツとして pr が割り当てられます。最初の値には pr の最初の値が割り当てられ、2 番目の値には pr の 2 番目の値が割り当てられます。

2) ペアを含む比較 (==) 演算子: 指定された 2 つのペア (pair1 とペア 2) について、比較演算子はこれら 2 つのペアの最初の値と 2 番目の値を比較します。つまり、pair1.first が pair2.first に等しいかどうか、また、pair1.second が pair2.second に等しいかどうかを比較します。 。

つまり、 if ( (pari1.first ==pair2.first) && (pair1.second==pair2.second) )

2 つの条件のいずれかが false の場合は false を返し、それ以外の場合は true を返します。

3) ペアを含む不等号 (!=) 演算子: 指定された 2 つのペア、たとえば、pair1 とペア 2 について、!= 演算子はこれら 2 つのペアの最初の値を比較します。つまり、pair1.first が pair2.first に等しいかどうか、等しい場合は両方の 2 番目の値をチェックします。

4) ペアを持つ論理 (>=, <= ) 演算子: 指定された 2 つのペア (たとえば、pair1 とペア 2) の場合、=、> はペアでも使用できます。ペアの最初の値のみを比較して、0 または 1 を返します。 p1=(1,20) および p2=(1,10) p2 のようなペアの場合 または

&list=PLqM7alHXFySGg6GSRmE2INI4k8fPH5qVB