vector嵌套vector嵌套pair

vector< vector<pair<int, int> > >的用法

通过简单的例子来理解vector和vector的嵌套

第一层vector< vector<pair<int, int> > >
第二层 vector<pair<int,int>>
第三层 pair<int ,int>

#include<iostream>
#include<vector>
using namespace std;
const int maxn=4;
int main(){
	
	//输入
	vector<vector<pair<int,int> > > vec;//最外层vector 
	for(int i=0;i<maxn;i++){
		vector<pair<int,int> > vec_in;//内层vector 
		for(int j=0;j<maxn;j++){
			pair<int,int> p;//最内层的pair 
			p=make_pair(i,j);//点对
			vec_in.push_back(p);
		}
		vec.push_back(vec_in);
	}
	cout<<"size of vec: "<<vec.size()<<endl;
	
	//输出
	vector<vector<pair<int,int> > >::iterator iter;//外层迭代器
	for(iter=vec.begin();iter!=vec.end();iter++){
		cout<<" vec一层 "<<endl;
		vector<pair<int,int> > temp=*iter;
		vector<pair<int,int> >::iterator it;//内层迭代器
		for(it=temp.begin();it!=temp.end();it++){
			cout<<"the value is: "<< (*it).first<<" " <<(*it).second<<endl;
		}	
	} 
}

测试结果

size of vec: 4
vec一层
the value is: 0 0
the value is: 0 1
the value is: 0 2
the value is: 0 3
vec一层
the value is: 1 0
the value is: 1 1
the value is: 1 2
the value is: 1 3
vec一层
the value is: 2 0
the value is: 2 1
the value is: 2 2
the value is: 2 3
vec一层
the value is: 3 0
the value is: 3 1
the value is: 3 2
the value is: 3 3

分析
输出
测试结果表明,最外层vector 有4个元素,每一个元素都是一个vector;第二层vector大小也是4,而且内容是点对(pair)。

构造过程
先形成点对(pair),然后把pair放入到内层vector,再把内层vector 放入到外层vector中。
举例
最小生成树(MST)prim算法使用到这个写法,特意记录一下

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值