#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;

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值