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算法使用到这个写法,特意记录一下