pair Structure

可以将两个对象作为单个对象的结构。

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

参数

  • _Val1
    初始化 pair的第一个元素值。

  • _Val2
    初始化 pair的第二个元素值。

  • _Right
    值用于初始化对另一个的元素对。

返回值

第一个(默认值)构造函数初始化匹配的第一个元素为类型 Type1 默认和第二个元素为类型 Type2默认值。

第二个构造函数初始化匹配的第一个元素。_Val1 和秒为 _Val2。

第三个(模板)构造函数初始化匹配的第一个元素。_Right。first 和秒为 _Right。second

使用 Rvalue引用声明:&&,第四个构造函数初始化匹配的第一个元素。_Val1 和秒为 _Val2

备注

模板结构存储类型 Type1Type2对象对,分别。 该类型 first_type 是作为模板参数 Type1 和类型 second_type 与模板参数 Type2相同。 Type1Type2 每个需要提供默认构造函数、单一参数构造函数和一个析构函数。 因为类型声明为 struct 而不是 class,该类型 pair 的所有成员都是公共的。 对的两个最常见的用途是返回两个值的函数的类型,所以,当具有一个键和值类型与每个元素关联的容器元素类别 映射选件类基于选件类。 后者满足对关联容器的要求和具有窗体 pair<constkey_typemapped_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;
   }
}
  
  
  
  
  
  

要求

标头: <utility>

命名空间: std

请参见

参考

Pair Logical Operator

线程安全性对标准C++库中