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

53 lines
1.1 KiB
C++
Raw Permalink Normal View History

2023-08-02 19:22:52 +08:00
// vector 的嵌套使用示例
#include <bits/stdc++.h>
using namespace std;
int main()
{
2023-08-02 19:23:29 +08:00
// 注意: vector<vector<int> > vs; 防止编辑器自动格式化为 >>
2023-08-02 19:22:52 +08:00
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;
}