简述

  • Vector的数据结构和数组非常相似,也称为单端数组
  • 不同于数据的静态空间,vector可以动态拓展(不是在原来的空间上拓展,而是找更大的空间,然后进行拷贝,释放原空间)

基本应用

创建

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main()
  5. {
  6. vector<int> v;
  7. vector<int> v2(v.begin(), v.end());//拷贝v的指定区间进行拷贝
  8. vector<int> v3(v);//拷贝v
  9. return 0;

容量及大小

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int> v;
  8. //增
  9. v.push_back(1);//尾部添加一个元素
  10. cout << v.empty() << endl;//容器是否为空
  11. cout << v.capacity() << endl;//获取容器大小
  12. cout << v.size() << endl;//获取元素数量
  13. v.resize(50);//设置容器的大小为50(如果容器变小了,那么超出的元素会被删除)
  14. v.resize(50, 10);////设置容器的大小为50,如果容器变小了,那么超出的元素会被删除,如果容器由短变长,则以10填充到新增区域
  15. return 0;
  16. }

增删查改

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int> v;
  8. //增
  9. v.push_back(1);//尾部添加一个元素
  10. v.insert(v.begin()+1, 3);//在指定为位置插入一个元素(案例为在index为1为地方添加3)
  11. v.insert(v.begin(),4,2);//在指定为位置插入一个元素(案例为在开始位置添加4个2)
  12. //遍历
  13. for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  14. {
  15. cout << *it << endl;
  16. }
  17. //查
  18. cout << v.at(1) << endl;//获取index为1的元素
  19. cout << v.front() << endl;//获取第一个元素
  20. cout << v.back() << endl;//获取最后一个元素
  21. //删
  22. v.erase(v.begin());//删除指定的元素
  23. v.erase(v.begin(), v.end()); //删除指定区间的元素
  24. v.pop_back();//删除尾部的元素
  25. v.clear();//清空
  26. //改
  27. v.operator[](0) = 29;//将第一个元素修改为29
  28. return 0;
  29. }

遍历及迭代

  • 方案一 ```cpp

    include

    include

    include

using namespace std;

int main() { vector v;

  1. v.assign(10, 199);//重置v的内容为10个100
  2. //第二种遍历的方法
  3. for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  4. {
  5. cout << *it << endl;
  6. }
  7. return 0;

}

  1. - 方案二
  2. ```cpp
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7. int main()
  8. {
  9. vector<int> v;
  10. v.push_back(1);
  11. v.push_back(2);
  12. //通过迭代器访问容器中的数据
  13. vector<int>::iterator itBegin = v.begin();//起始迭代器,指向容器中第一个元素
  14. vector<int>::iterator itEnd = v.end();//结束迭代器,指向容器中最后一个元素的后一位
  15. while (itBegin != itEnd)
  16. {
  17. cout << *itBegin << endl;
  18. itBegin++;
  19. }
  20. return 0;
  21. }
  • 方案二 ```c

    include

    include

    include

    include

using namespace std;

int main() { vector v; v.push_back(1); v.push_back(2);//往后添加一个数字

  1. //通过迭代器访问容器中的数据
  2. vector<int>::iterator itBegin = v.begin();//起始迭代器,指向容器中第一个元素
  3. vector<int>::iterator itEnd = v.end();//结束迭代器,指向容器中最后一个元素的后一位
  4. while (itBegin != itEnd)
  5. {
  6. cout << *itBegin << endl;
  7. itBegin++;
  8. }

v.assign(10, 199);//重置v的内容为10个100 //第二种遍历的方法 for (vector::iterator it = v.begin(); it != v.end(); it++) { cout << *it << endl; }

  1. return 0;

}

  1. <a name="ecLvJ"></a>
  2. ## 其他
  3. <a name="rdif3"></a>
  4. ### 设置vector的预留空间
  5. 由于动态空间的缺点是数据量较大的时候,可能会出现多次的内存拓展及拷贝,那么在已知到这种情况的条件下,我们可以给vector设置一个相对较大的初始容量。
  6. ```cpp
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10. using namespace std;
  11. int main()
  12. {
  13. vector<int> v;
  14. v.reserve(1000);//设置预留容量
  15. cout << "capacity:" << v.capacity() << endl;//1000
  16. cout << "size:" << v.size() << endl;//0
  17. for (int i = 0; i < 1000;i++){
  18. v.push_back(i);
  19. }
  20. cout << "capacity:" << v.capacity() << endl;//1000,因为空间刚好够,不会进行扩容
  21. cout << "size:" << v.size() << endl;//1000
  22. return 0;
  23. }

capacity:1000 size:0 capacity:1000 size:1000

两个vector的内容互换

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int> v;
  8. v.push_back(1);
  9. vector<int> v2;
  10. v2.push_back(2);
  11. v2.swap(v);//将v和v2的内容互换
  12. return 0;
  13. }

利用swap方法实现自动收缩内存空间

  1. using namespace std;
  2. int main()
  3. {
  4. vector<int> v;
  5. for (int i = 0; i < 1000;i++){
  6. v.push_back(i);
  7. }
  8. cout << "capacity:" << v.capacity() << endl;//大于1000
  9. cout << "size:" << v.size() << endl;//1000
  10. v.resize(3);
  11. cout << "capacity:" << v.capacity() << endl;//大于1000,capacity并不会修改,内存还是占用着
  12. cout << "size:" << v.size() << endl;//3
  13. //利用swap进行收缩空间,匿名对象用完立刻就被释放了,而v由于拷贝自己的内容,故而不变,但capacity经过交换已经变成了和size一样的大小
  14. vector<int>(v).swap(v);
  15. cout << "capacity:" << v.capacity() << endl;//3
  16. cout << "size:" << v.size() << endl;//3
  17. return 0;
  18. }

capacity:1024 size:1000 capacity:1024 size:3 capacity:3 size:3