Struttura pair

Struct che permette di considerare due oggetti come un singolo oggetto.

Sintassi

struct pair
{
    typedef T1 first_type;
    typedef T2 second_type;
    T1 first;
    T2 second;
    constexpr pair();
    pair(const pair&) = default;
    pair(pair&&) = default;
    constexpr pair(
        const T1& Val1,
        const T2& Val2);

    template <class Other1, class Other2>
    constexpr pair(const pair<Other1, Other2>& Right);

    template <class Other1, class Other2>
    constexpr pair(const pair <Other1 Val1, Other2 Val2>&& Right);

    template <class Other1, class Other2>
    constexpr pair(Other1&& Val1, Other2&& Val2);

    template <class... Args1, class... Args2>
    pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args);

    pair& operator=(const pair& p);
    template<class U1, class U2> pair& operator=(const pair<U1, U2>& p);
    pair& operator=(pair&& p) noexcept(see below );
    template<class U1, class U2> pair& operator=(pair<U1, U2>&& p);

    void swap(pair& p) noexcept(see below );
};

template<class T1, class T2>
    pair(T1, T2) -> pair<T1, T2>;

Parametri

Val1
Valore che inizializza il primo elemento di pair.

Val2
Valore che inizializza il secondo elemento di pair.

Right
Coppia i cui valori devono essere usati per inizializzare gli elementi di un'altra coppia.

Valore restituito

Il primo costruttore (impostazione predefinita) inizializza il primo elemento della coppia sul valore predefinito di tipo T1 e il secondo elemento per impostazione predefinita di tipo T2. Viene definito se entrambi i tipi sono costruttibili per impostazione predefinita.

Il secondo costruttore inizializza il primo elemento della coppia a Val1 e il secondo a Val2. Viene definito se entrambi i tipi sono copiabili.

Il terzo costruttore (modello) inizializza il primo elemento della coppia in Right. first e il secondo in Right. second. Viene definito se entrambi i tipi della coppia sono costruiscibili dai tipi valore forniti.

Il quarto costruttore inizializza il primo elemento della coppia a Val1 e il secondo a Val2 usando il dichiaratore di riferimento Rvalue: &&. Viene definito se entrambi i tipi della coppia sono costruiscibili dai tipi valore forniti.

Osservazioni:

Lo struct modello archivia rispettivamente una coppia di oggetti di tipo T1 e T2. Il tipo first_type è uguale al parametro del modello T1 e il tipo second_type è uguale al parametro del modello T2. T1 e T2 ogni esigenza specifica solo un costruttore predefinito, un costruttore a argomento singolo e un distruttore. Tutti i membri del tipo pair sono pubblici, perché il tipo viene dichiarato come anziché struct come .class Una coppia viene usata in genere in due modi: come tipo restituito per le funzioni che restituiscono due valori e come elemento per le classi contenitore associative map e multimap, entrambe con una chiave e un tipo di valore associati a ciascun elemento. Quest'ultimo soddisfa i requisiti per un contenitore associativo di coppia e ha un tipo di valore del formato pair< const key_type, mapped_type >.

Esempio

// 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;
   }
}
The pair p1 is: ( 10, 0.011 ).
The pair p2 is: ( 10, 0.222 ).
The pair p3 is: ( 10, 0.011 ).
The element pairs of the map m1 are: ( 1, 10 ) ( 2, 20 ) ( 3, 30 ).
The element (4,40) was inserted successfully in m1.
The element with a key value of
( (pr2.first) -> first ) = 1 is already in m1,
so the insertion failed.