Freigeben über


pair Structure

Eine Struktur, die bereitstellt, sodass die Möglichkeit zwei Objekte als einzelnes Objekt behandelt.

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
      );
   };

Parameter

  • _Val1
    Wert, der das erste Element aus pair initialisiert.

  • _Val2
    Wert, der das zweite Element von pair initialisiert.

  • _Right
    Ein Paar, dessen Werte verwendet werden soll, um die Elemente einer anderen Paare zu initialisieren.

Rückgabewert

Der erste (Standard) Konstruktor initialisiert erstes Element des Paars auf dem des Typs Type1 und des zweiten Elements auf den Standard des Typs Type2.

Der zweite Konstruktor initialisiert erstes Element des Paars zu _Val1 und second zu _Val2.

Der dritte (Vorlagen) Konstruktor initialisiert erstes Element des Paars zu _Right.first und second zu _Right.second.

Der vierte Konstruktor initialisiert erstes Element des Paars zu _Val1 und second zu _Val2 mithilfe Rvalu-Verweis-Deklarator: &&.

Hinweise

Die Vorlagenstruktur speichert ein Paar von Objekten des Typs Type1 und Type2, bzw.Der Typ first_type ist gleich, den der Vorlagenparameter Type1 und der Typ second_type identisch mit Vorlagenparameter Type2 ist.Type1 und Type2 jede Anforderung geben nur einen Standardkonstruktor, einen Einzelargumentkonstruktors aufgerufen und einen Destruktor an.Alle Member des Typs pair sind öffentlich, da der Typ als struct nicht als class deklariert wird.Die beiden häufigste Verwendung für ein Paar ist als Rückgabetypen für Funktionen, die zwei Werte zurückgeben und als Elemente für die assoziative Containerklassen Zuordnung Klasse und Multimap Klasse, die einen Schlüssel und einen Werttyp verfügen, die mit jedem Element zugeordnet sind.Der zweite Vorgang wird den Anforderungen für einen assoziative Container des Paars erfüllt und verfügt über einen Werttyp Formular pair<constkey_type, mapped_type>.

Beispiel

// 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;
   }
}
  
  
  
  
  
  

Anforderungen

Header: <utility>

Namespace: std

Siehe auch

Referenz

Pair Logical Operator

Threadsicherheit in der C++-Standardbibliothek