Udostępnij za pośrednictwem


pair — Struktura

Struct, która przewiduje możliwość traktowania dwa obiekty jako pojedynczy obiekt.

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

Parametry

  • _Val1
    Wartość inicjowanie pierwszego elementu pair.

  • _Val2
    Wartość inicjowanie drugim elemencie pair.

  • _Right
    Para, których wartości mają być użyte do zainicjowania elementy inną parę.

Wartość zwracana

Pierwszy element pary do domyślnego typu inicjuje pierwszy Konstruktor (ustawienie domyślne) Type1 i drugi element domyślny typ Type2.

Drugi Konstruktor inicjuje pierwszy element pary do _Val1 i drugi do _Val2.

Pierwszy element danej parze inicjuje trzeciego Konstruktor (szablon) _Right. pierwszy i drugi do _Right.second.

Czwarty Konstruktor inicjuje pierwszy element pary do _Val1 i drugi do _Val2 za pomocą Deklarator odwołania do wartości R: &&.

Uwagi

Struktura szablonu przechowuje parę obiektów typu Type1 i Type2, odpowiednio.Typ first_type jest taka sama, jak parametr szablonu Type1 i typu second_type jest taka sama, jak parametr szablonu Type2.Type1 i Type2 każdy musi dostarczać konstruktora domyślnego, Konstruktor pojedynczy argument i destruktora.Wszystkie elementy członkowskie typu pair są publiczne, ponieważ typ został zadeklarowany jako struct , a nie jako klasy.Dwie najbardziej typowe zastosowania dla pary są jak zwracanych typów dla funkcji zwracających dwie wartości i jako elementy dla klas asocjacyjne kontenera mapowania klasy i multimap klasy mają zarówno klucz, jak i typ wartości skojarzone z każdym elementem.Ten ostatni spełnia wymagania dla kontenera asocjacyjne pary i ma wartość typu formularza pair<constkey_type, mapped_type>. 

Przykład

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

Wymagania

Nagłówek:<narzędzie>

Przestrzeń nazw: std

Zobacz też

Informacje

Pair — Operator logiczny

Bezpieczeństwo wątku w standardowej bibliotece C++