赞
踩
- 2.array,vector 的讲解
- 2.1array的详细讲解(静态数组)
- #include<iostream>
- #include<stdlib.h>
- #include<array>
- #include<vector>
- using namespace std;
- void main()
- {
- array<int, 5> myint1 = { 1,2,3,4,5 };//创建一维包含5个元素的整形数组
- array<int, 5> myint2 = { 11,12,13,14,15 };//创建一维包含5个元素的整形数组
- array<int, 5> myint3 = { 21,22,23,24,25 };//创建一维包含5个元素的整形数组
- //下面两种是等价的
- array < array<int, 5>, 3 > myint= {myint1, myint2, myint3}; //创建二维数组,其中为3行5列的整形数组
- array < array<int, 5>, 3 > myint = { 1,2,3,4,5,11,12,13,14,15,21,22,23,24,25 };//创建二维数组,其中为3行5列的整形数组
-
- //打印出数组
- for (int i = 0;i < 3;i++)
- {
- for (int j = 0;j < 5;j++)
- {
- cout << myint[i][j] << " ";
- }
- cout << endl;
- }
- }
-
-
- 2.2Vector的详细讲解(动态数组)
- 2.2.1
- void main()
- {
- vector<int> myvector;//动态数组
-
- vector<string> string1;//动态字符串数组
- string1.push_back("notepad");
- string1.push_back("cala");
- string1.push_back("mspaint");
- //string1.clear();//清空
- //string1.size();//大小
- for (int i = 0;i < string1.size();i++)
- {
- cout<<string1[i]<<endl;
- }
- }
-
- 2.2.2 迭代器
- /************Vector迭代器************/
-
- void main()
- {
- vector<string> string1;//动态字符串数组
- string1.push_back("notepad");
- string1.push_back("cala");
- string1.push_back("mspaint");
-
- vector<string>::iterator ibegin, iend; //迭代器
- ibegin = string1.begin(); //数据结构的起始点
- iend = string1.end(); //结束
- for (;ibegin!=iend;ibegin++)
- {
- string tempsrt = *ibegin;//获取指针指向的数据
- system(tempsrt.c_str());//执行指令
- }
- }
-
-
-
- /************Array迭代器************/
- void main()
- {
- array <int, 5> myint = { 1,2,3,4,5 };
- array<int, 5> ::iterator ibegin, iend; 正向迭代器
- ibegin = myint.begin();
- iend = myint.end();
- for (;ibegin != iend;ibegin++)
- {
- cout << *ibegin << " " << endl;
- }
-
- //********************************/
- array<int, 5>::reverse_iterator rbegin, rend;反向迭代器
- rbegin = myint.rbegin();
- rend = myint.rend();
- for (;rbegin != rend;rbegin++)
- {
- cout << *rbegin << " " << endl;
- }
- }
-
-
-
-
- void main()
- {
- vector<int> myvector(5); //分配5个空间
- //增
- myvector.push_back(1);
- myvector.push_back(11);
- myvector.push_back(111);
- myvector.push_back(11111);
- myvector.push_back(2);
-
- myvector.pop_back();//删除最后一个元素
-
- myvector.erase(myvector.begin());//删除第1个
- myvector.erase(myvector.begin()+3);//删除第4个
- myvector.erase(myvector.begin()+5);//删除第6个
- //myvector.clear();//删除所有元素
-
- for (int i=0;i<myvector.size();i++)
- {
- cout << myvector.at(i)<< " " << endl;
- }
- }
-
-
-
-
-
-
-
-
-
- void main()
- {
- vector <int> myvector1;
- myvector1.push_back(1);
- myvector1.push_back(2);
- myvector1.push_back(3);
-
- vector <int> myvector2;
- myvector2.push_back(22);
-
- vector <int> myvector3;
- myvector3.push_back(33);
-
- vector <vector<int>> allvector;
- allvector.push_back(myvector1);
- allvector.push_back(myvector2);
- allvector.push_back(myvector3);
-
- for (int i=0;i<allvector.size();i++)
- {
- for (int j = 0;j < allvector[i].size();j++)
- {
- cout << allvector[i][j]<<" " ;
- }
- cout << endl;
- }
-
- }
-
-
-
-
-
-
- void main()
- {
- array<double, 4> mydouble = { 1,2,3,4 };
- /* 利用迭代器
- array<double, 4> ::iterator ibegin, iend;
- ibegin = mydouble.begin();
- iend = mydouble.end();
- for (;ibegin != iend;ibegin++)
- {
- cout <<*ibegin<<" ";
- }
- cout << endl;
-
- */
- 使用常规方法
- for (int i=0;i<mydouble.size();i++)
- {
- cout << mydouble[i] << " ";
- }
- cout << endl;
-
- std::cin.get();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。