Vector
1.特点:vector是C++标准模板库中的内容,它具有可变长度、可存取任意数据类型的特点,使用vector可以构建类似多维数组的数据结构。
2.常用方法:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> test;
test.push_back(8);
test.push_back(5);
test.push_back(3);
test.push_back(7); //尾部加入元素
reverse(test.begin(), test.end()); //反转vector
for (vector<int>::iterator it = test.begin(); it != test.end(); ++it)
cout << *it << endl; //使用迭代器遍历
for (int i = 0; i < test.size(); ++i)
cout << test[i] << endl; //使用索引遍历
cout << test.size() << endl; //返回v的长度
test.pop_back(); //删除v最后一个元素,类似于弹栈,因此可用来实现栈
cout << test.back() << endl; //取最后一个元素
vector<int>::iterator it = test.begin(); //声明迭代器
it = test.erase(it); //删除迭代器it指