C++编程->pair(对组)

本文详细介绍了C++中pair类型的定义、初始化、赋值和常用操作,包括默认构造、复制构造、移动构造以及初始化方式。同时展示了如何通过pair进行元素交换、比较和输出,提供了丰富的代码实例。

 pair 是 一种模版类型。每个pair 可以存储两个值。这两种值无限制,可以是tuple,vector ,string,struct等等。

首先来看一下pair的函数

初始化,复制等相关操作如下:

default (1)
constexpr pair();
copy / move (2)
template<class U, class V> pair (const pair<U,V>& pr);
template<class U, class V> pair (pair<U,V>&& pr);
pair (const pair& pr) = default;
pair (pair&& pr) = default;
initialization (3)
pair (const first_type& a, const second_type& b);
template<class U, class V> pair (U&& a, V&& b);
piecewise (4)
template <class... Args1, class... Args2>
  pair (piecewise_construct_t pwc, tuple<Args1...> first_args,
                                   tuple<Args2...> second_args);

	// pair TEMPLATE FUNCTIONS
//交换函数
template<class _Ty1,
	class _Ty2> inline
	void swap(pair<_Ty1, _Ty2>& _Left, pair<_Ty1, _Ty2>& _Right)
	{	// swap _Left and _Right pairs
	_Left.swap(_Right);
	}
//判断是否相等函数
template<class _Ty1,
	class _Ty2> inline
	bool operator==(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test for pair equality
	return (_Left.first == _Right.first && _Left.second == _Right.second);//两个元素都比较
	}
//判断是否不等函数
template<class _Ty1,
	class _Ty2> inline
	bool operator!=(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test for pair inequality
	return (!(_Left == _Right));
	}
//判断是否小于函数
template<class _Ty1,
	class _Ty2> inline
	bool operator<(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left < _Right for pairs
	return (_Left.first < _Right.first ||
		!(_Right.first < _Left.first) && _Left.second < _Right.second);
	}
//判断是否大于函数
template<class _Ty1,
	class _Ty2> inline
	bool operator>(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left > _Right for pairs
	return (_Right < _Left);
	}
//判断是否小于等于函数
template<class _Ty1,
	class _Ty2> inline
	bool operator<=(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left <= _Right for pairs
	return (!(_Right < _Left));
	}
//判断是否大于等于函数
template<class _Ty1,
	class _Ty2> inline
	bool operator>=(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left >= _Right for pairs
	return (!(_Left < _Right));
	}

贴一段代码:

//pair 定义
	pair<string,int>pair1;

	//pair 定义以及赋值一
	pair<string,int>pair2("lily",4);
	pair<string,int>pair3(pair2);

	//pair 赋值方式二
	pair1=make_pair(string("tom"),3);

	//pair 赋值方式三
	pair1.first="jim";
	pair1.second=2;

	//pair 赋值方式四
	get<0>(pair1)=string("jim");
	get<1>(pair1)=6;

	//pair 赋值方式五
	swap(pair1,pair3);

	//pair 输出方式一
	cout<<pair2.first<<endl;
	cout<<pair2.second<<endl;

	//pair 输出方式二
	cout<<get<0>(pair1)<<endl;
	cout<<get<1>(pair1)<<endl;


c++:# P1238 走迷宫 ## 题目描述 有一个 $m\times n$ 格的迷宫(表示有 $m$ 行、$n$ 列),其中有可走的也有不可走的,如果用 $1$ 表示可以走,$0$ 表示不可以走,文件读入这 $m\times n$ 个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。如果一条路都不可行,则输出相应信息(用 $-1$ 表示无路)。 优先顺序:左上右下。**数据保证随机生成。** ## 输入格式 第一行是两个数 $m,n(1<m,n<15)$,接下来是 $m$ 行 $n$ 列由 $1$ 和 $0$ 成的数据,最后两行是起始点和结束点。 ## 输出格式 所有可行的路径,描述一个点时用 $(x,y)$ 的形式,除开始点外,其他的都要用 `->` 表示方向。 如果没有一条可行的路则输出 $-1$。 ## 输入输出样例 #1 ### 输入 #1 ``` 5 6 1 0 0 1 0 1 1 1 1 1 1 1 0 0 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 5 6 ``` ### 输出 #1 ``` (1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6) (1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6) (1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6) (1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6) (1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6) (1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6) (1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6) (1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6) (1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6) (1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6) (1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6) (1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6) ``` ## 说明/提示 数据保证随机生成。事实上,如果 $n=m=14$ 且每个位置都是 $1$ 的话,有 $69450664761521361664274701548907358996488$ 种路径。
最新发布
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值