bilibiliC++31-37_STL常用容器_vector容器

短命女 2022-10-30 02:28 307阅读 0赞

3.2 vector容器

3.2.1 vector基本概念

功能:

  • vector数据结构和数组非常相似,也称为单端数组

vector与普通数组区别:

  • 不同之处在于数组是静态空间,而vector可以动态扩展

动态扩展:

  • 并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间

在这里插入图片描述

  • vector容器的迭代器是支持随机访问的迭代器

3.2.2 vector构造函数

功能描述:

  • 创建vector容器

函数原型:

  • vector<T> v; //采用模板实现类实现,默认构造函数
  • vector(v.begin(), v.end()); //将v[begin(), end())区间中的元素拷贝给本身。
  • vector(n, elem); //构造函数将n个elem拷贝给本身。
  • vector(const vector &vec); //拷贝构造函数。

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <vector>
  5. void printVector(vector<int>& v) {
  6. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  7. cout << *it << " ";
  8. }
  9. cout << endl;
  10. }
  11. void test01()
  12. {
  13. vector<int> v1; //无参构造
  14. for (int i = 0; i < 10; i++)
  15. {
  16. v1.push_back(i);
  17. }
  18. printVector(v1);
  19. vector<int> v2(v1.begin(), v1.end());
  20. printVector(v2);
  21. vector<int> v3(10, 100);
  22. printVector(v3);
  23. vector<int> v4(v3);
  24. printVector(v4);
  25. }
  26. int main() {
  27. test01();
  28. system("pause");
  29. return 0;
  30. }
  31. /* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 请按任意键继续. . . */

总结: vector的多种构造方式没有可比性,灵活使用即可

3.2.3 vector赋值操作

功能描述:

  • 给vector容器进行赋值

函数原型:

  • vector& operator=(const vector &vec);//重载等号操作符
  • assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
  • assign(n, elem); //将n个elem拷贝赋值给本身。

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <vector>
  5. void printVector(vector<int>& v) {
  6. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  7. cout << *it << " ";
  8. }
  9. cout << endl;
  10. }
  11. //赋值操作
  12. void test01()
  13. {
  14. vector<int> v1; //无参构造
  15. for (int i = 0; i < 10; i++)
  16. {
  17. v1.push_back(i);
  18. }
  19. printVector(v1);
  20. vector<int>v2;
  21. v2 = v1;
  22. printVector(v2);
  23. vector<int>v3;
  24. v3.assign(v1.begin(), v1.end());
  25. printVector(v3);
  26. vector<int>v4;
  27. v4.assign(10, 100);
  28. printVector(v4);
  29. }
  30. int main() {
  31. test01();
  32. system("pause");
  33. return 0;
  34. }
  35. /* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 100 100 100 100 100 100 100 100 100 100 请按任意键继续. . . */

总结: vector赋值方式比较简单,使用operator=,或者assign都可以

3.2.4 vector容量和大小

功能描述:

  • 对vector容器的容量和大小操作

函数原型:

  • empty(); //判断容器是否为空
  • capacity(); //容器的容量
  • size(); //返回容器中元素的个数
  • resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。

    ​ //如果容器变短,则末尾超出容器长度的元素被删除。

  • resize(int num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。

    ​ //如果容器变短,则末尾超出容器长度的元素被删除

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <vector>
  5. void printVector(vector<int>& v) {
  6. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  7. cout << *it << " ";
  8. }
  9. cout << endl;
  10. }
  11. void test01()
  12. {
  13. vector<int> v1;
  14. for (int i = 0; i < 10; i++)
  15. {
  16. v1.push_back(i);
  17. }
  18. printVector(v1);
  19. if (v1.empty())
  20. {
  21. cout << "v1为空" << endl;
  22. }
  23. else
  24. {
  25. cout << "v1不为空" << endl;
  26. cout << "v1的容量 = " << v1.capacity() << endl;
  27. cout << "v1的大小 = " << v1.size() << endl;
  28. }
  29. //resize 重新指定大小 ,若指定的更大,默认用0填充新位置,可以利用重载版本替换默认填充
  30. v1.resize(15, 10);
  31. printVector(v1);
  32. //resize 重新指定大小 ,若指定的更小,超出部分元素被删除
  33. v1.resize(5);
  34. printVector(v1);
  35. }
  36. int main() {
  37. test01();
  38. system("pause");
  39. return 0;
  40. }
  41. /*0 1 2 3 4 5 6 7 8 9 v1不为空 v1的容量 = 13 v1的大小 = 10 0 1 2 3 4 5 6 7 8 9 10 10 10 10 10 0 1 2 3 4 请按任意键继续. . .*/

总结:

  • 判断是否为空 — empty
  • 返回元素个数 — size
  • 返回容器容量 — capacity
  • 重新指定大小 — resize

3.2.5 vector插入和删除

功能描述:

  • 对vector容器进行插入、删除操作

函数原型:

  • push_back(ele); //尾部插入元素ele
  • pop_back(); //删除最后一个元素
  • insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele
  • insert(const_iterator pos, int count,ele);//迭代器指向位置pos插入count个元素ele
  • erase(const_iterator pos); //删除迭代器指向的元素
  • erase(const_iterator start, const_iterator end);//删除迭代器从start到end之间的元素
  • clear(); //删除容器中所有元素

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <vector>
  5. void printVector(vector<int>& v) {
  6. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  7. cout << *it << " ";
  8. }
  9. cout << endl;
  10. }
  11. //插入和删除
  12. void test01()
  13. {
  14. vector<int> v1;
  15. //尾插
  16. v1.push_back(10);
  17. v1.push_back(20);
  18. v1.push_back(30);
  19. v1.push_back(40);
  20. v1.push_back(50);
  21. printVector(v1);
  22. //尾删
  23. v1.pop_back();
  24. printVector(v1);
  25. //插入
  26. v1.insert(v1.begin(), 100);
  27. printVector(v1);
  28. v1.insert(v1.begin(), 2, 1000);
  29. printVector(v1);
  30. //删除
  31. v1.erase(v1.begin());
  32. printVector(v1);
  33. //清空
  34. v1.erase(v1.begin(), v1.end());
  35. v1.clear();
  36. printVector(v1);
  37. }
  38. int main() {
  39. test01();
  40. system("pause");
  41. return 0;
  42. }
  43. /* 10 20 30 40 50 10 20 30 40 100 10 20 30 40 1000 1000 100 10 20 30 40 1000 100 10 20 30 40 请按任意键继续. . .*/

总结:

  • 尾插 — push_back
  • 尾删 — pop_back
  • 插入 — insert (位置迭代器)
  • 删除 — erase (位置迭代器)
  • 清空 — clear

3.2.6 vector数据存取

功能描述:

  • 对vector中的数据的存取操作

函数原型:

  • at(int idx); //返回索引idx所指的数据
  • operator[]; //返回索引idx所指的数据
  • front(); //返回容器中第一个数据元素
  • back(); //返回容器中最后一个数据元素

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <vector>
  5. void test01()
  6. {
  7. vector<int>v1;
  8. for (int i = 0; i < 10; i++)
  9. {
  10. v1.push_back(i);
  11. }
  12. for (int i = 0; i < v1.size(); i++)
  13. {
  14. cout << v1[i] << " ";
  15. }
  16. cout << endl;
  17. for (int i = 0; i < v1.size(); i++)
  18. {
  19. cout << v1.at(i) << " ";
  20. }
  21. cout << endl;
  22. cout << "v1的第一个元素为: " << v1.front() << endl;
  23. cout << "v1的最后一个元素为: " << v1.back() << endl;
  24. }
  25. int main() {
  26. test01();
  27. system("pause");
  28. return 0;
  29. }
  30. /* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 v1的第一个元素为: 0 v1的最后一个元素为: 9 请按任意键继续. . . */

总结:

  • 除了用迭代器获取vector容器中元素,[ ]和at也可以
  • front返回容器第一个元素
  • back返回容器最后一个元素

3.2.7 vector互换容器

功能描述:

  • 实现两个容器内元素进行互换

函数原型:

  • swap(vec); // 将vec与本身的元素互换

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <vector>
  5. void printVector(vector<int>& v) {
  6. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  7. cout << *it << " ";
  8. }
  9. cout << endl;
  10. }
  11. void test01()
  12. {
  13. vector<int>v1;
  14. for (int i = 0; i < 10; i++)
  15. {
  16. v1.push_back(i);
  17. }
  18. printVector(v1);
  19. vector<int>v2;
  20. for (int i = 10; i > 0; i--)
  21. {
  22. v2.push_back(i);
  23. }
  24. printVector(v2);
  25. //互换容器
  26. cout << "互换后" << endl;
  27. v1.swap(v2);
  28. printVector(v1);
  29. printVector(v2);
  30. }
  31. void test02()
  32. {
  33. vector<int> v;
  34. for (int i = 0; i < 100000; i++) {
  35. v.push_back(i);
  36. }
  37. cout << "v的容量为:" << v.capacity() << endl;
  38. cout << "v的大小为:" << v.size() << endl;
  39. v.resize(3);
  40. cout << "v的容量为:" << v.capacity() << endl;
  41. cout << "v的大小为:" << v.size() << endl;
  42. //收缩内存
  43. vector<int>(v).swap(v); //匿名对象
  44. cout << "v的容量为:" << v.capacity() << endl;
  45. cout << "v的大小为:" << v.size() << endl;
  46. }
  47. int main() {
  48. test01();
  49. test02();
  50. system("pause");
  51. return 0;
  52. }
  53. /* 0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 互换后 10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 v的容量为:138255 v的大小为:100000 v的容量为:138255 v的大小为:3 v的容量为:3 v的大小为:3 请按任意键继续. . . */

在这里插入图片描述

总结:swap可以使两个容器互换,可以达到实用的收缩内存效果

3.2.8 vector预留空间

功能描述:

  • 减少vector在动态扩展容量时的扩展次数

函数原型:

  • reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问。

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <vector>
  5. void test01()
  6. {
  7. vector<int> v;
  8. //预留空间
  9. v.reserve(100000);
  10. //统计开辟多少次内存
  11. int num = 0;//统计开辟次数
  12. int* p = NULL;
  13. for (int i = 0; i < 100000; i++) {
  14. v.push_back(i);
  15. if (p != &v[0]) {
  16. p = &v[0];
  17. num++;
  18. }
  19. }
  20. cout << "num:" << num << endl;
  21. }
  22. int main() {
  23. test01();
  24. system("pause");
  25. return 0;
  26. }
  27. /* num:1 请按任意键继续. . . */

总结:如果数据量较大,可以一开始利用reserve预留空间

发表评论

表情:
评论列表 (有 0 条评论,307人围观)

还没有评论,来说两句吧...

相关阅读