C++ //vector容器嵌套容器

 1 //vector容器嵌套容器
 2 #include <iostream>
 3 #include <string>
 4 #include<fstream>
 5 #include<vector>
 6 #include<algorithm>
 7 using namespace std;
 8 
 9 void test01()
10 {
11     vector<vector<int>>v;
12 
13     //创建小容器
14     vector<int>v1;
15     vector<int>v2;
16     vector<int>v3;
17     vector<int>v4;
18 
19 
20     //向小容器种添加数据
21     for (int i = 0; i < 4; i++)
22     {
23         v1.push_back(i + 1);
24         v2.push_back(i + 2);
25         v3.push_back(i + 3);
26         v4.push_back(i + 4);
27     }
28     //将小容器插入到大的容器种
29     v.push_back(v1);
30     v.push_back(v2);
31     v.push_back(v3);
32     v.push_back(v4);
33 
34 
35     //通过大容器,把所有数据遍历一边
36     for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
37     {
38         //(*it) ----容器 vector<int>
39         for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
40         {
41             cout << *vit << " ";
42         }
43         cout << endl;
44     }
45 
46 }
47 
48 
49 int main()
50 {
51 
52     test01();
53 
54 
55     system("pause");
56 
57     return 0;
58 
59 }

 

posted on 2021-08-13 17:43  Bytezero!  阅读(403)  评论(0编辑  收藏  举报