Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

pair Structure

A struct that provides for the ability to treat two objects as a single object.

template<class T1, class T2>
   struct pair 
   {
   typedef T1 first_type;
   typedef T2 second_type;
   T1 first;
   T2 second;

   constexpr pair( );
   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
      );
   };

Val1

Value initializing the first element of pair.

Val2

Value initializing the second element of pair.

Right

A pair whose values are to be used to initialize the elements of another pair.

The first (default) constructor initializes first element of the pair to the default of type T1 and second element to default of type T2.

The second constructor initializes first element of the pair to Val1 and second to Val2.

The third (template) constructor initializes first element of the pair to Right.first and second to Right.second.

The fourth constructor initializes first element of the pair to Val1 and second to Val2 using Rvalue Reference Declarator: &&.

The template struct stores a pair of objects of type T1 and T2, respectively. The type first_type is the same as the template parameter T1 and the type second_type is the same as the template parameter T2. T1 and T2 each need supply only a default constructor, a single-argument constructor, and a destructor. All members of the type pair are public, because the type is declared as a struct rather than as a class. The two most common uses for a pair are as return types for functions that return two values and as elements for the associative container classes map Class and multimap Class that have both a key and a value type associated with each element. The latter satisfies the requirements for a pair associative container and has a value type of the form pair<const key_type, mapped_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;
   }
}
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.

Header: <utility>

Namespace: std

Show:
© 2015 Microsoft