Share via


pair Structure

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

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
      );
   void swap(pair<Type1, Type2>& _Right);
   };

Parameters

  • _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.

  • _Right
    A pair to exchange contents with.

Return Value

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

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.

Remarks

The template struct stores a pair of objects of type Type1 and Type2, respectively. The type first_type is the same as the template parameter Type1 and the type second_type is the same as the template parameter Type2. Type1 and Type2 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>.

Example

// 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.

Requirements

Header: <utility>

Namespace: std

See Also

Reference

Pair Logical Operator

Thread Safety in the Standard C++ Library

Other Resources

<utility> Members