简述
- Vector的数据结构和数组非常相似,也称为单端数组
- 不同于数据的静态空间,vector可以动态拓展(不是在原来的空间上拓展,而是找更大的空间,然后进行拷贝,释放原空间)
基本应用
创建
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
vector<int> v2(v.begin(), v.end());//拷贝v的指定区间进行拷贝
vector<int> v3(v);//拷贝v
return 0;
容量及大小
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
//增
v.push_back(1);//尾部添加一个元素
cout << v.empty() << endl;//容器是否为空
cout << v.capacity() << endl;//获取容器大小
cout << v.size() << endl;//获取元素数量
v.resize(50);//设置容器的大小为50(如果容器变小了,那么超出的元素会被删除)
v.resize(50, 10);////设置容器的大小为50,如果容器变小了,那么超出的元素会被删除,如果容器由短变长,则以10填充到新增区域
return 0;
}
增删查改
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
//增
v.push_back(1);//尾部添加一个元素
v.insert(v.begin()+1, 3);//在指定为位置插入一个元素(案例为在index为1为地方添加3)
v.insert(v.begin(),4,2);//在指定为位置插入一个元素(案例为在开始位置添加4个2)
//遍历
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}
//查
cout << v.at(1) << endl;//获取index为1的元素
cout << v.front() << endl;//获取第一个元素
cout << v.back() << endl;//获取最后一个元素
//删
v.erase(v.begin());//删除指定的元素
v.erase(v.begin(), v.end()); //删除指定区间的元素
v.pop_back();//删除尾部的元素
v.clear();//清空
//改
v.operator[](0) = 29;//将第一个元素修改为29
return 0;
}
遍历及迭代
using namespace std;
int main()
{
vector
v.assign(10, 199);//重置v的内容为10个100
//第二种遍历的方法
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}
return 0;
}
- 方案二
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
//通过迭代器访问容器中的数据
vector<int>::iterator itBegin = v.begin();//起始迭代器,指向容器中第一个元素
vector<int>::iterator itEnd = v.end();//结束迭代器,指向容器中最后一个元素的后一位
while (itBegin != itEnd)
{
cout << *itBegin << endl;
itBegin++;
}
return 0;
}
using namespace std;
int main()
{
vector
//通过迭代器访问容器中的数据
vector<int>::iterator itBegin = v.begin();//起始迭代器,指向容器中第一个元素
vector<int>::iterator itEnd = v.end();//结束迭代器,指向容器中最后一个元素的后一位
while (itBegin != itEnd)
{
cout << *itBegin << endl;
itBegin++;
}
v.assign(10, 199);//重置v的内容为10个100
//第二种遍历的方法
for (vector
return 0;
}
<a name="ecLvJ"></a>
## 其他
<a name="rdif3"></a>
### 设置vector的预留空间
由于动态空间的缺点是数据量较大的时候,可能会出现多次的内存拓展及拷贝,那么在已知到这种情况的条件下,我们可以给vector设置一个相对较大的初始容量。
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
v.reserve(1000);//设置预留容量
cout << "capacity:" << v.capacity() << endl;//1000
cout << "size:" << v.size() << endl;//0
for (int i = 0; i < 1000;i++){
v.push_back(i);
}
cout << "capacity:" << v.capacity() << endl;//1000,因为空间刚好够,不会进行扩容
cout << "size:" << v.size() << endl;//1000
return 0;
}
capacity:1000 size:0 capacity:1000 size:1000
两个vector的内容互换
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(1);
vector<int> v2;
v2.push_back(2);
v2.swap(v);//将v和v2的内容互换
return 0;
}
利用swap方法实现自动收缩内存空间
using namespace std;
int main()
{
vector<int> v;
for (int i = 0; i < 1000;i++){
v.push_back(i);
}
cout << "capacity:" << v.capacity() << endl;//大于1000
cout << "size:" << v.size() << endl;//1000
v.resize(3);
cout << "capacity:" << v.capacity() << endl;//大于1000,capacity并不会修改,内存还是占用着
cout << "size:" << v.size() << endl;//3
//利用swap进行收缩空间,匿名对象用完立刻就被释放了,而v由于拷贝自己的内容,故而不变,但capacity经过交换已经变成了和size一样的大小
vector<int>(v).swap(v);
cout << "capacity:" << v.capacity() << endl;//3
cout << "size:" << v.size() << endl;//3
return 0;
}
capacity:1024 size:1000 capacity:1024 size:3 capacity:3 size:3