vector:底层结构---动态顺序表
//vector的使用;
#include<vector>
using namespace std;
int main()
{
vector<int> v1;
vector<int> v2(10,5);//(n,字符);
//区间构造;
vector<int> v3(v2.begin(),v2.end());
int array[] = { 1,2,3,4,5,6,7,8,9,0 };
vector<int> v4(array,array+sizeof(array)/sizeof(array[0]));
vector<int>v5(v4);
for (size_t i = 0; i < v2.size(); ++i)//遍历
cout << v2[i] << " ";
cout << endl;
// 采用迭代器遍历;
auto it = v3.begin();
while (it != v3.end())
{
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : v4)//简化遍历
cout << e << " ";
cout << endl;
auto rit = v5.rbegin();
while (rit != v5.rend())
{
cout << *rit << " ";
//cout << endl;
++rit;
}
system("pause");
return 0;
}
#include<vector>
using namespace std;
int main()
{
//vector<char> v1;
int arr[] = { 0,1,2,3,4,5,6,7,8,9 };
vector<int> v2(arr,arr+sizeof(arr)/sizeof(arr[0]));
v2.reserve(15);
v2.push_back(1);
v2.push_back(2);
v2.push_back(3);
auto it = v2.begin();
while (it != v2.end())
{
cout << *it << " ";
++it;
}
cout << endl;
reverse(v2.begin(), v2.end());
for (auto e : v2)//简化遍历
cout << e << " ";
cout << endl;
/*auto it = v2.begin();
while (it != v2.end())
{
cout << *it << " ";
++it;
}
cout << endl;
*/
//影响迭代器的操作;
//resize;reserve;push_back;insert;
//迭代器失效,解决办法;给迭代器重新赋值;
cout << v2.back() << endl;
system("pause");
return 0;
}