#include <utility>

#include <utility>这个头文件是什么用法

utility头文件定义了一个pair类型,是标准库的一部分,其原型为:
template<class _Ty1,
class _Ty2> struct pair
{ // store a pair of values
typedef pair<_Ty1, _Ty2> _Myt;
typedef _Ty1 first_type;
typedef _Ty2 second_type;

pair()
: first(_Ty1()), second(_Ty2())
{ // construct from defaults
}

pair(const _Ty1& _Val1, const _Ty2& _Val2)
: first(_Val1), second(_Val2)
{ // construct from specified values
}

template<class _Other1,
class _Other2>
pair(const pair<_Other1, _Other2>& _Right)
: first(_Right.first), second(_Right.second)
{ // construct from compatible pair
}

void swap(_Myt& _Right)
{ // exchange contents with _Right
if (this != &_Right)
{ // different, worth swapping
std::swap(first, _Right.first);
std::swap(second, _Right.second);
}
}

_Ty1 first; // the first stored value
_Ty2 second; // the second stored value
};
可以看出这是一个模板类,定义了两个数据成员:first、second和一组成员函数。
有两个普通构造函数和一个复制构造函数,具体的还定义了一个make_pair函数和一些操作符,
函数的实现你可以看看标准库的utility头文件。另外,map的元素类型就是pair类型~



例子

#include <iostream>

#include <utility>

using namespace std;

pair<int, int> p;

int main()

{

cin >> p.first >> p.second;

cout << p.first << " " << p.second << endl;

return 0;

}


原文地址:https://www.cnblogs.com/Renyi-Fan/p/7568234.html