C++ STL //vector容器存放内置数组


 1 //STL初始
 2 // 
 3 //vector容器存放内置数组
 4 
 5 #include <iostream>
 6 #include <string>
 7 #include<fstream>
 8 using namespace std;
 9 #include<vector>
10 #include<algorithm>//标准算法的头文件
11 
12 //vector容器存放内置数组
13 
14 void myPrint(int val)
15 {
16     cout << val << endl;
17 }
18 void test01()
19 {
20     //创建了一个vecor容器,数组
21     vector<int> v;
22 
23     //向容器长插入数据
24     v.push_back(10);
25     v.push_back(20);
26     v.push_back(30);
27     v.push_back(40);
28 
29     //通过迭代器访问容器中的数据
30     //vector<int>::iterator itBegin = v.begin();//起始迭代器 指向容器中第一个元素
31     //vector<int>::iterator itEnd = v.end();//结束迭代器 指向容器中最后一个元素的下一个位置
32 
33     ////1.编译方式
34     //while (itBegin != itEnd)
35     //{
36     //    cout << *itBegin << endl;
37     //    itBegin++;
38     //}
39 
40 
41     ////第二种遍历方式
42     //for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
43     //{
44     //    cout << *it << endl;
45     //}
46 
47 
48     //第三种 利用STL提供遍历算法
49     for_each(v.begin(), v.end(), myPrint);
50 
51 }
52 
53 
54 int main()
55 {
56     test01();
57 
58 
59 
60     system("pause");
61 
62     return 0;
63 
64 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM