make_pair (STL Samples)

在 Visual C++ 演示如何使用 make_pair 标准 (STL)模板库函数。

template<class first, class second> inline
   pair<first,
      second> make_pair(
      const first& _X,
      const second& _Y
   )

备注

备注

类/参数名在原型不匹配版本在头文件。修改某些提高可读性。

make_pair STL 函数创建包含任何类型的两个数据元素的一对结构。

示例

// mkpair.cpp
// compile with: /EHsc
// Illustrates how to use the make_pair function.
//
// Functions: make_pair - creates an object pair containing two data
//                        elements of any type.

#include <utility>
#include <iostream>

using namespace std;

/* STL pair data type containing int and float
*/

typedef struct pair<int,float> PAIR_IF;

int main(void)
{
  PAIR_IF pair1=make_pair(18,3.14f);

  cout << pair1.first << "  " << pair1.second << endl;
  pair1.first=10;
  pair1.second=1.0f;
  cout << pair1.first << "  " << pair1.second << endl;
}

Output

18  3.14
10  1

要求

**标题:**utility

请参见

概念

标准模板库示例