qfedu-cpp-level/day8/stl_code/d4.cpp

52 lines
1015 B
C++

// vector 的嵌套使用示例
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<vector<int> > vs; // 嵌套容器
vector<int> v1, v2, v3;
for (int i = 0; i < 9; i++)
{
switch (i / 3)
{
case 0:
v1.push_back(i + 1);
break;
case 1:
v2.push_back(i + 1);
break;
case 2:
v3.push_back(i + 1);
break;
}
}
vs.push_back(v1);
vs.push_back(v2);
vs.push_back(v3);
// for (int i = 0; i < vs.size(); i++)
// {
// for (int j = 0; j < vs[i].size(); j++)
// {
// cout << vs[i][j] << " ";
// }
// cout << endl;
// }
vector<vector<int> >::iterator it;
for (it = vs.begin(); it != vs.end(); it++)
{
vector<int>::iterator it2;
for (it2 = (*it).begin(); it2 != (*it).end(); it2++)
{
cout << *it2 << " ";
}
cout << endl;
}
return 0;
}