• 深入C++ Vector:解密vector的奥秘与底层模拟实现揭秘


    W...Y的主页 😊

    代码仓库分享 💕


    🍔前言:

    我们学习了STL中的string以及其所有重要接口并进行了模拟实现,但是STL中包含的内容不止于此。学习了string之后继续学习STL中的vector,学习成本会大大降低,因为他们非现类似,现在就让我们进入vector的世界中吧!

    目录

    vector的介绍及使用

    vector的介绍

    vector的使用

     vector的定义

    vector iterator 的使用

    vector 空间增长问题

    vector 增删查改

     ​编辑

    vector的深度剖析以及模拟实现

    vector类的创建以及构造函数与析构函数

     迭代器相关模拟实现

     容量相关模拟实现

    元素访问相关模拟实现

    vector的修改操作模拟实现

     vector 迭代器失效问题

    赋值重载函数的模拟


    vector的介绍及使用

    vector的介绍

    1. vector是表示可变大小数组的序列容器。
    2. 就像数组一样,vector也采用的连续存储空间来存储元素。也就是意味着可以采用下标对vector的元素进行访问,和数组一样高效。但是又不像数组,它的大小是可以动态改变的,而且它的大小会被容器自动处理。
    3. 本质讲,vector使用动态分配数组来存储它的元素。当新元素插入时候,这个数组需要被重新分配大小为了增加存储空间。其做法是,分配一个新的数组,然后将全部元素移到这个数组。就时间而言,这是一个相对代价高的任务,因为每当一个新的元素加入到容器的时候,vector并不会每次都重新分配大小。
    4. vector分配空间策略:vector会分配一些额外的空间以适应可能的增长,因为存储空间比实际需要的存储空间更大。不同的库采用不同的策略权衡空间的使用和重新分配。但是无论如何,重新分配都应该是对数增长的间隔大小,以至于在末尾插入一个元素的时候是在常数时间的复杂度完成的。
    5. 因此,vector占用了更多的存储空间,为了获得管理存储空间的能力,并且以一种有效的方式动态增长。
    6. 与其它动态序列容器相比(deque, list and forward_list), vector在访问元素的时候更加高效,在末尾添加和删除元素相对高效。对于其它不在末尾的删除和插入操作,效率更低。比起list和forward_list统一的迭代器和引用更好。
     

    vector的使用

    vector学习时一定要学会查看文档:vector的文档介绍,vector在实际中非常的重要,在实际中我们熟悉常见的接口就可以,下面列出了哪些接口是要重点掌握的。

     vector的定义

    (constructor)构造函数声明 接口说明
    vector()(重点) 无参构造
    vector(size_type n, const value_type& val = value_type())构造并初始化n个val
    vector (const vector& x); (重点) 拷贝构造
    vector (InputIterator first, InputIterator last); 使用迭代器进行初始化构造

    这些vector定义参数全都是被typedef的内容,我们应该了解每个参数的含义: 下面演示以下如何使用构造函数与拷贝构造函数:

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. using namespace std;
    4. #include
    5. // vector的构造
    6. int TestVector1()
    7. {
    8. // constructors used in the same order as described above:
    9. vector<int> first; // empty vector of ints
    10. vector<int> second(4, 100); // four ints with value 100
    11. vector<int> third(second.begin(), second.end()); // iterating through second
    12. vector<int> fourth(third); // a copy of third
    13. // 下面涉及迭代器初始化的部分,我们学习完迭代器再来看这部分
    14. // the iterator constructor can also be used to construct from arrays:
    15. int myints[] = { 16,2,77,29 };
    16. vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int));
    17. cout << "The contents of fifth are:";
    18. for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
    19. cout << ' ' << *it;
    20. cout << '\n';
    21. return 0;
    22. }

    这里要强调一下迭代器构造函数,我们一般看到的类型是iterator类型的,而模板这里的模板参数给予的是inputiterator,并且给与class模板:

    迭代器也是分类型的,不仅仅只有string、vector迭代器,还有其他的迭代器。所以我们可以传入不同的迭代器对vector进行初始化操作。数组就是一个非常好的例子,在上述例子中我们也体现出不同迭代器对vector的初始化。 

    vector iterator 的使用

    iterator的使用接口说明
    begin +end(重点)获取第一个数据位置的iterator/const_iterator, 获取最后一个数据的下一个位置
    的iterator/const_iterator
    rbegin + rend获取最后一个数据位置的reverse_iterator,获取第一个数据前一个位置的
    reverse_iterator

     迭代器都是左闭右开的区间。

    1. void PrintVector(const vector<int>& v)
    2. {
    3. // const对象使用const迭代器进行遍历打印
    4. vector<int>::const_iterator it = v.begin();
    5. while (it != v.end())
    6. {
    7. cout << *it << " ";
    8. ++it;
    9. }
    10. cout << endl;
    11. }
    12. void TestVector2()
    13. {
    14. // 使用push_back插入4个数据
    15. vector<int> v;
    16. v.push_back(1);
    17. v.push_back(2);
    18. v.push_back(3);
    19. v.push_back(4);
    20. // 使用迭代器进行遍历打印
    21. vector<int>::iterator it = v.begin();
    22. while (it != v.end())
    23. {
    24. cout << *it << " ";
    25. ++it;
    26. }
    27. cout << endl;
    28. // 使用迭代器进行修改
    29. it = v.begin();
    30. while (it != v.end())
    31. {
    32. *it *= 2;
    33. ++it;
    34. }
    35. // 使用反向迭代器进行遍历再打印
    36. // vector::reverse_iterator rit = v.rbegin();
    37. auto rit = v.rbegin();
    38. while (rit != v.rend())
    39. {
    40. cout << *rit << " ";
    41. ++rit;
    42. }
    43. cout << endl;
    44. PrintVector(v);
    45. }

    上述代码我们使用迭代器对vector进行了正向与反向的遍历打印,很好的说明了迭代器的使用。我们也可以使用[]重载进行遍历,但这里我们不推荐使用,因为下标访问对底层逻辑是数组的可以进行访问,但是在后面的链表、树中就不能了,我们要尽早习惯使用迭代器。

    vector 空间增长问题

    容量空间 接口说明
    size获取数据个数
    capacity获取容量大小
    empty判断是否为空
    resize改变vector的size
    reserve改变vector的capacity

    vector这些接口与string是一模一样,只要学会使用string的接口vector的这些接口也不再话下:

    1. void TestVector3()
    2. {
    3. vector<int> v;
    4. // set some initial content:
    5. for (int i = 1; i < 10; i++)
    6. v.push_back(i);
    7. v.resize(5);
    8. v.resize(8, 100);
    9. v.resize(12);
    10. cout << "v contains:";
    11. for (size_t i = 0; i < v.size(); i++)
    12. cout << ' ' << v[i];
    13. cout << '\n';
    14. }
    15. // 测试vector的默认扩容机制
    16. // vs:按照1.5倍方式扩容
    17. // linux:按照2倍方式扩容
    18. void TestVectorExpand()
    19. {
    20. size_t sz;
    21. vector<int> v;
    22. sz = v.capacity();
    23. cout << "making v grow:\n";
    24. for (int i = 0; i < 100; ++i)
    25. {
    26. v.push_back(i);
    27. if (sz != v.capacity())
    28. {
    29. sz = v.capacity();
    30. cout << "capacity changed: " << sz << '\n';
    31. }
    32. }
    33. }
    34. // 往vecotr中插入元素时,如果大概已经知道要存放多少个元素
    35. // 可以通过reserve方法提前将容量设置好,避免边插入边扩容效率低
    36. void TestVectorExpandOP()
    37. {
    38. vector<int> v;
    39. size_t sz = v.capacity();
    40. v.reserve(100); // 提前将容量设置好,可以避免一遍插入一遍扩容
    41. cout << "making bar grow:\n";
    42. for (int i = 0; i < 100; ++i)
    43. {
    44. v.push_back(i);
    45. if (sz != v.capacity())
    46. {
    47. sz = v.capacity();
    48. cout << "capacity changed: " << sz << '\n';
    49. }
    50. }
    51. }

     vector的扩容与string的扩容机制是一样的,都是vs下是1.5倍扩容增长,Linux下是2倍增长。

    vector中有一个函数接口我们可以有所了解,这个函数是用来缩容的。如果size的大小为8,而capacity的大小为80,我们可以使用shrink_to_fit函数进行缩容。但是我们不建议缩容,因为会进行空间的深拷贝以及析构。有所了解即可。

    注意:reserve只负责开辟空间,如果确定知道需要用多少空间,reserve可以缓解vector增容的代价缺陷问题。resize在开空间的同时还会进行初始化,影响size。

    vector 增删查改

    vector增删查改接口说明

    push_back

    尾插
    pop_back尾删
    find查找。(注意这个是算法模块实现,不是vector的成员接口)
    insert在position之前插入val
    erase删除position位置的数据
    swap交换两个vector的数据空间
    operator[]像数组一样访问

     

     insert与erase中与string有区别,在string中支持使用下标进行访问,而在vector中只支持迭代器进行访问。

    find查找函数在vector中是没有的,而包含在algorithm头文件中

    这样我们每次使用find都必须包含算法头文件,但是find函数是一个模板函数,所以只要是迭代器无论是什么类型的都可以进行复用!!!

    剩下的接口与string是一样的,使用起来非常简单,下面是演示代码:

    1. // 尾插和尾删:push_back/pop_back
    2. void TestVector4()
    3. {
    4. vector<int> v;
    5. v.push_back(1);
    6. v.push_back(2);
    7. v.push_back(3);
    8. v.push_back(4);
    9. auto it = v.begin();
    10. while (it != v.end())
    11. {
    12. cout << *it << " ";
    13. ++it;
    14. }
    15. cout << endl;
    16. v.pop_back();
    17. v.pop_back();
    18. it = v.begin();
    19. while (it != v.end())
    20. {
    21. cout << *it << " ";
    22. ++it;
    23. }
    24. cout << endl;
    25. }
    26. // 任意位置插入:insert和erase,以及查找find
    27. // 注意find不是vector自身提供的方法,是STL提供的算法
    28. void TestVector5()
    29. {
    30. // 使用列表方式初始化,C++11新语法
    31. vector<int> v{ 1, 2, 3, 4 };
    32. // 在指定位置前插入值为val的元素,比如:3之前插入30,如果没有则不插入
    33. // 1. 先使用find查找3所在位置
    34. // 注意:vector没有提供find方法,如果要查找只能使用STL提供的全局find
    35. auto pos = find(v.begin(), v.end(), 3);
    36. if (pos != v.end())
    37. {
    38. // 2. 在pos位置之前插入30
    39. v.insert(pos, 30);
    40. }
    41. vector<int>::iterator it = v.begin();
    42. while (it != v.end())
    43. {
    44. cout << *it << " ";
    45. ++it;
    46. }
    47. cout << endl;
    48. pos = find(v.begin(), v.end(), 3);
    49. // 删除pos位置的数据
    50. v.erase(pos);
    51. it = v.begin();
    52. while (it != v.end()) {
    53. cout << *it << " ";
    54. ++it;
    55. }
    56. cout << endl;
    57. }
    58. // operator[]+index 和 C++11中vector的新式for+auto的遍历
    59. // vector使用这两种遍历方式是比较便捷的。
    60. void TestVector6()
    61. {
    62. vector<int> v{ 1, 2, 3, 4 };
    63. // 通过[]读写第0个位置。
    64. v[0] = 10;
    65. cout << v[0] << endl;
    66. // 1. 使用for+[]小标方式遍历
    67. for (size_t i = 0; i < v.size(); ++i)
    68. cout << v[i] << " ";
    69. cout << endl;
    70. vector<int> swapv;
    71. swapv.swap(v);
    72. cout << "v data:";
    73. for (size_t i = 0; i < v.size(); ++i)
    74. cout << v[i] << " ";
    75. cout << endl;
    76. // 2. 使用迭代器遍历
    77. cout << "swapv data:";
    78. auto it = swapv.begin();
    79. while (it != swapv.end())
    80. {
    81. cout << *it << " ";
    82. ++it;
    83. }
    84. // 3. 使用范围for遍历
    85. for (auto x : v)
    86. cout << x << " ";
    87. cout << endl;
    88. }

    vector的深度剖析以及模拟实现

    要实现vector我们先要从STL中了解vector的底层逻辑。

    上图就是vector在STL中的源代码,其中就有许多不知名的参数在vector中的使用我们也能看到,为了更好的理解,这些都是被typedef的。接下来我们可以看到类中的参数并不是我们以前学习到的T* tmp、int size以及int capacity,而是用三个指针进行的,分别是start、finish以及end_of_storage所体现的。

    这三个指针分别代表着首指针,内容尾部指针以及空间尾部指针,与size、capacity有着密切的关联,这样说还不够明显,我们接着往下看。

    vector中的size与capacity函数的源代码,就是将提供私有成员进行相减得到的大小。我们就可以理解其中的start、finish、end_of_storage的指向了。 

     其实大体的结构没有改变,只是使用指针去定义vector中的各种数据。

    现在我们就可以进行vector的模拟实现了。

    vector类的创建以及构造函数与析构函数

    1. #pragma once
    2. #include
    3. using namespace std;
    4. #include
    5. namespace why
    6. {
    7. template<class T>
    8. class vector
    9. {
    10. public:
    11. // Vector的迭代器是一个原生指针
    12. typedef T* iterator;
    13. typedef const T* const_iterator;
    14. ///
    15. // 构造和销毁
    16. vector()
    17. : _start(nullptr)
    18. , _finish(nullptr)
    19. , _endOfStorage(nullptr)
    20. {}
    21. vector(size_t n, const T& value = T())
    22. : _start(nullptr)
    23. , _finish(nullptr)
    24. , _endOfStorage(nullptr)
    25. {
    26. reserve(n);
    27. while (n--)
    28. {
    29. push_back(value);
    30. }
    31. }
    32. /*
    33. * 理论上将,提供了vector(size_t n, const T& value = T())之后
    34. * vector(int n, const T& value = T())就不需要提供了,但是对于:
    35. * vector v(10, 5);
    36. * 编译器在编译时,认为T已经被实例化为int,而10和5编译器会默认其为int类型
    37. * 就不会走vector(size_t n, const T& value = T())这个构造方法,
    38. * 最终选择的是:vector(InputIterator first, InputIterator last)
    39. * 因为编译器觉得区间构造两个参数类型一致,因此编译器就会将InputIterator实例化为int
    40. * 但是10和5根本不是一个区间,编译时就报错了
    41. * 故需要增加该构造方法
    42. */
    43. vector(int n, const T& value = T())
    44. : _start(new T[n])
    45. , _finish(_start+n)
    46. , _endOfStorage(_finish)
    47. {
    48. for (int i = 0; i < n; ++i)
    49. {
    50. _start[i] = value;
    51. }
    52. }
    53. // 若使用iterator做迭代器,会导致初始化的迭代器区间[first,last)只能是vector的迭代器
    54. // 重新声明迭代器,迭代器区间[first,last)可以是任意容器的迭代器
    55. template<class InputIterator>
    56. vector(InputIterator first, InputIterator last)
    57. {
    58. while (first != last)
    59. {
    60. push_back(*first);
    61. ++first;
    62. }
    63. }
    64. ~vector()
    65. {
    66. if (_start)
    67. {
    68. delete[] _start;
    69. _start = _finish = _endOfStorage = nullptr;
    70. }
    71. }
    72. //拷贝构造函数
    73. vector(const vector& v)
    74. : _start(nullptr)
    75. , _finish(nullptr)
    76. , _endOfStorage(nullptr)
    77. {
    78. reserve(v.capacity());
    79. iterator it = begin();
    80. const_iterator vit = v.cbegin();
    81. while (vit != v.cend())
    82. {
    83. *it++ = *vit++;
    84. }
    85. _finish = it;
    86. }
    87. private:
    88. iterator _start; // 指向数据块的开始
    89. iterator _finish; // 指向有效数据的尾
    90. iterator _endOfStorage; // 指向存储容量的尾
    91. };
    92. }

    这是vector常见的构造函数与析构函数。

    无参默认函数非常简单,而第二种构造函数是将一种类型的内容进行n个初始化,那为什么在模拟构造时要写两个此类函数构成重载吗?不是多此一举?

    因为假如不写重载就会与第三个迭代器类模板冲突,因为我们传入的参数很可能是两个int类型的值,我们用户本意是将n个int类型的值进行初始化,但是两个int值会与更匹配的模板进行结合,导致非法间接寻址,所以我们必须要重载一个int型。

     迭代器相关模拟实现

    1. iterator begin()
    2. {
    3. return _start;
    4. }
    5. iterator end()
    6. {
    7. return _finish;
    8. }
    9. const_iterator cbegin() const
    10. {
    11. return _start;
    12. }
    13. const_iterator cend() const
    14. {
    15. return _finish;
    16. }

     容量相关模拟实现

    1. size_t size() const
    2. {
    3. return _finish - _start;
    4. }
    5. size_t capacity() const
    6. {
    7. return _endOfStorage - _start;
    8. }
    9. bool empty() const
    10. {
    11. return _start == _finish;
    12. }
    13. void reserve(size_t n)
    14. {
    15. if (n > capacity())
    16. {
    17. size_t oldSize = size();
    18. // 1. 开辟新空间
    19. T* tmp = new T[n];
    20. // 2. 拷贝元素
    21. // 这里直接使用memcpy会有问题吗?同学们思考下
    22. //if (_start)
    23. // memcpy(tmp, _start, sizeof(T)*size);
    24. if (_start)
    25. {
    26. for (size_t i = 0; i < oldSize; ++i)
    27. tmp[i] = _start[i];
    28. // 3. 释放旧空间
    29. delete[] _start;
    30. }
    31. _start = tmp;
    32. _finish = _start + oldSize;
    33. _endOfStorage = _start + n;
    34. }
    35. }
    36. void resize(size_t n, const T& value = T())
    37. {
    38. // 1.如果n小于当前的size,则数据个数缩小到n
    39. if (n <= size())
    40. {
    41. _finish = _start + n;
    42. return;
    43. }
    44. // 2.空间不够则增容
    45. if (n > capacity())
    46. reserve(n);
    47. // 3.将size扩大到n
    48. iterator it = _finish;
    49. _finish = _start + n;
    50. while (it != _finish)
    51. {
    52. *it = value;
    53. ++it;
    54. }
    55. }

     reserve函数是扩容函数,在复用的时候肯定会遇到异地扩容的情况,所以我们必须进行深拷贝处理,使用memcpy可以解决一些普通变量的拷贝比如:int、double等等。但是面对复杂的内容就无法解决,所以我们必须使用赋值进行拷贝。

    假设模拟实现的vector中的reserve接口中,使用memcpy进行的拷贝,以下代码会发生什么问题?

    1. int main()
    2. {
    3. bite::vector v;
    4. v.push_back("1111");
    5. v.push_back("2222");
    6. v.push_back("3333");
    7. return 0;
    8. }

    问题分析:
    1. memcpy是内存的二进制格式拷贝,将一段内存空间中内容原封不动的拷贝到另外一段内存空间中
    2. 如果拷贝的是自定义类型的元素,memcpy既高效又不会出错,但如果拷贝的是自定义类型元素,并且自定义类型元素中涉及到资源管理时,就会出错,因为memcpy的拷贝实际是浅拷贝。 

    结论:如果对象中涉及到资源管理时,千万不能使用memcpy进行对象之间的拷贝,因为memcpy是浅拷贝,否则可能会引起内存泄漏甚至程序崩溃。 

    我们必须打好reserve的基础,这个扩容在后面的许多函数都必须要用,我们必须创建好。

    resize的函数算法与string中的算法原理相同。

    元素访问相关模拟实现

    1. T& operator[](size_t pos)
    2. {
    3. assert(pos < size());
    4. return _start[pos];
    5. }
    6. const T& operator[](size_t pos)const
    7. {
    8. assert(pos < size());
    9. return _start[pos];
    10. }
    11. T& front()
    12. {
    13. return *_start;
    14. }
    15. const T& front()const
    16. {
    17. return *_start;
    18. }
    19. T& back()
    20. {
    21. return *(_finish - 1);
    22. }
    23. const T& back()const
    24. {
    25. return *(_finish - 1);
    26. }

    在不改变内容的情况下,我们必须考虑有const的,所以必须进行函数重载。

    vector的修改操作模拟实现

    1. void push_back(const T& x)
    2. {
    3. insert(end(), x);
    4. }
    5. void pop_back()
    6. {
    7. erase(end() - 1);
    8. }
    9. void swap(vector& v)
    10. {
    11. std::swap(_start, v._start);
    12. std::swap(_finish, v._finish);
    13. std::swap(_endOfStorage, v._endOfStorage);
    14. }
    15. iterator insert(iterator pos, const T& x)
    16. {
    17. assert(pos <= _finish);
    18. // 空间不够先进行增容
    19. if (_finish == _endOfStorage)
    20. {
    21. //size_t size = size();
    22. size_t newCapacity = (0 == capacity()) ? 1 : capacity() * 2;
    23. reserve(newCapacity);
    24. // 如果发生了增容,需要重置pos
    25. pos = _start + size();
    26. }
    27. iterator end = _finish - 1;
    28. while (end >= pos)
    29. {
    30. *(end + 1) = *end;
    31. --end;
    32. }
    33. *pos = x;
    34. ++_finish;
    35. return pos;
    36. }
    37. // 返回删除数据的下一个数据
    38. // 方便解决:一边遍历一边删除的迭代器失效问题
    39. iterator erase(iterator pos)
    40. {
    41. // 挪动数据进行删除
    42. iterator begin = pos + 1;
    43. while (begin != _finish) {
    44. *(begin - 1) = *begin;
    45. ++begin;
    46. }
    47. --_finish;
    48. return pos;
    49. }

    在创建insert与erase函数时,我们都会遇到一种问题,迭代器失效。

     vector 迭代器失效问题

    迭代器的主要作用就是让算法能够不用关心底层数据结构,其底层实际就是一个指针,或者是对指针进行了封装,比如:vector的迭代器就是原生态指针T* 。因此迭代器失效,实际就是迭代器底层对应指针所指向的空间被销毁了,而使用一块已经被释放的空间,造成的后果是程序崩溃(即如果继续使用已经失效的迭代器,程序可能会崩溃)。

    对于vector可能会导致其迭代器失效的操作有:
    1. 会引起其底层空间改变的操作,都有可能是迭代器失效,比如:resize、reserve、insert、assign、push_back等。

    1. #include
    2. using namespace std;
    3. #include
    4. int main()
    5. {
    6. vector<int> v{1,2,3,4,5,6};
    7. auto it = v.begin();
    8. // 将有效元素个数增加到100个,多出的位置使用8填充,操作期间底层会扩容
    9. // v.resize(100, 8);
    10. // reserve的作用就是改变扩容大小但不改变有效元素个数,操作期间可能会引起底层容量改变
    11. // v.reserve(100);
    12. // 插入元素期间,可能会引起扩容,而导致原空间被释放
    13. // v.insert(v.begin(), 0);
    14. // v.push_back(8);
    15. // 给vector重新赋值,可能会引起底层容量改变
    16. v.assign(100, 8);
    17. /*
    18. 出错原因:以上操作,都有可能会导致vector扩容,也就是说vector底层原理旧空间被释放掉,
    19. 而在打印时,it还使用的是释放之间的旧空间,在对it迭代器操作时,实际操作的是一块已经被释放的
    20. 空间,而引起代码运行时崩溃。
    21. 解决方式:在以上操作完成之后,如果想要继续通过迭代器操作vector中的元素,只需给it重新
    22. 赋值即可。
    23. */
    24. while(it != v.end())
    25. {
    26. cout<< *it << " " ;
    27. ++it;
    28. }
    29. cout<
    30. return 0;
    31. }

    指定位置元素的删除操作--erase

    1. #include
    2. using namespace std;
    3. #include
    4. int main()
    5. {
    6. int a[] = { 1, 2, 3, 4 };
    7. vector<int> v(a, a + sizeof(a) / sizeof(int));
    8. // 使用find查找3所在位置的iterator
    9. vector<int>::iterator pos = find(v.begin(), v.end(), 3);
    10. // 删除pos位置的数据,导致pos迭代器失效。
    11. v.erase(pos);
    12. cout << *pos << endl; // 此处会导致非法访问
    13. return 0;
    14. }

     erase删除pos位置元素后,pos位置之后的元素会往前搬移,没有导致底层空间的改变,理论上讲迭代器不应该会失效,但是:如果pos刚好是最后一个元素,删完之后pos刚好是end的位置,而end位置是没有元素的,那么pos就失效了。因此删除vector中任意位置上元素时,vs就认为该位置迭代器失效了。

    Linux下,g++编译器对迭代器失效的检测并不是非常严格,处理也没有vs下极端。

    迭代器失效解决办法:在使用前,对迭代器重新赋值即可。

    赋值重载函数的模拟
    1. void swap(vector& v)
    2. {
    3. std::swap(_start, v._start);
    4. std::swap(_finish, v._finish);
    5. std::swap(_endOfStorage, v._endOfStorage);
    6. }
    7. vector& operator=(vector v)
    8. {
    9. swap(v);
    10. return *this;
    11. }

    我们可以偷个懒,将拷贝好的内容直接进行交换即可实现赋值的作用。

    以上我们将vector与vector的模拟实现全部完成。相信大家看完这篇博客可以对vector有更深的理解。

    创作不易,希望大家多多支持!!!

  • 相关阅读:
    基于虚拟阻抗的下垂控制——孤岛双机并联Simulink仿真
    知识蒸馏2:目标检测中的知识蒸馏
    腾讯云服务器linux+CentOS7.9+yum源+nginx搭建网站
    博客与AI
    iOS关于列表布局的几种实现方式小结
    MyBatisPlus(十七)通用枚举
    真正“搞”懂HTTP协议13之HTTP2
    Day 85
    HCIP—BGP邻居关系建立实验
    以汇川中型PLC(AM系列)为例,CODESYS平台变量与字节数组互转的多种方法
  • 原文地址:https://blog.csdn.net/m0_74755811/article/details/134436718