[C++ Learning] STL container-vector

Table of contents

1. Introduction and use of vector

1.1 Introduction to vector

1.2 Use of vector

1.2.1 Definition of vector

1.2.2 Use of vector iterator

 1.2.3 Vector space growth problem

1.2.4 vector addition, deletion, checking and modification

1.2.5 Vector iterator failure problem (key points)

2. In-depth analysis and simulation implementation of vectorEdit

2.1 Simulated implementation of the core framework interface of std::vector casso::vector

2.2 Problems using memcpy to copy

2.3 Understanding dynamic two-dimensional arrays


1. Introduction and use of vector

1.1 Introduction to vector

        1. Vector is a sequence container that represents a variable-size array.
        2. Just like arrays, vectors also use continuous storage space to store elements. This means that you can use subscripts to access the elements of the vector , which is as efficient as an array. But unlike an array, its size can be changed dynamically, and its size will be automatically .
        3. Essentially, vector uses a dynamically allocated array to store its elements. When new elements are inserted, the array needs to be resized to increase storage space. This is done by allocating a new array and then moving all elements into this array. In terms of time, this is a relatively expensive task, because the vector is not resized every time a new element is added to the container.
        4. Vector allocation space strategy: vector will allocate some additional space to accommodate possible growth because the storage space is larger than the actual required storage space. Different libraries use different strategies to trade off space usage and reallocation. But in any case, the reallocation should be logarithmically increasing in interval size, so that inserting an element at the end is done in constant time.
        5. Therefore, vector takes up more storage space in order to gain the ability to manage storage space and grow dynamically in an efficient manner.
        6. Compared with other dynamic sequence containers ( deque, list and forward_list ), vector is more efficient when accessing elements, and adding and deleting elements at the end is relatively efficient. For other deletion and insertion operations that are not at the end, the efficiency is even lower. It is better to use unified iterators and references than list and forward_list .
        There are three realms of using STL : being able to use it, understanding it, and being able to expand it.

1.2 Use of vector

        When learning vector, you must learn to check the documentation: Introduction to vector documentation , vector is very important in practice. In practice, we only need to be familiar with common interfaces. The following lists which interfaces we should focus on mastering .
1.2.1 Definition of vector
(constructor) Constructor declaration Interface Description
vector() (emphasis) No-argument construction
vectorsize_type n, const value_type& val=value_type() Construct and initialize n vals
vector (const vector& x); (emphasis) copy construction
vector (InputIterator first, InputIterator last); Initialization using iterators
Vector construction code demonstration:
#include <iostream>
using namespace std;
#include <vector>


//    vector的构造

int TestVector1()
{
    // constructors used in the same order as described above:
    vector<int> first;                                // empty vector of ints
    vector<int> second(4, 100);                       // four ints with value 100
    vector<int> third(second.begin(), second.end());  // iterating through second
    vector<int> fourth(third);                       // a copy of third

    // 下面涉及迭代器初始化的部分,我们学习完迭代器再来看这部分
    // the iterator constructor can also be used to construct from arrays:
    int myints[] = { 16,2,77,29 };
    vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int));

    cout << "The contents of fifth are:";
    for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
        cout << ' ' << *it;
    cout << '\n';

    return 0;
}



//  vector的迭代器

void PrintVector(const vector<int>& v)
{
	// const对象使用const迭代器进行遍历打印
	vector<int>::const_iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

void TestVector2()
{
	// 使用push_back插入4个数据
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);

	// 使用迭代器进行遍历打印
	vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	// 使用迭代器进行修改
	it = v.begin();
	while (it != v.end())
	{
		*it *= 2;
		++it;
	}

	// 使用反向迭代器进行遍历再打印
	// vector<int>::reverse_iterator rit = v.rbegin();
	auto rit = v.rbegin();
	while (rit != v.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;

	PrintVector(v);
}


//  vector的resize 和 reserve

// reisze(size_t n, const T& data = T())
// 将有效元素个数设置为n个,如果时增多时,增多的元素使用data进行填充
// 注意:resize在增多元素个数时可能会扩容
void TestVector3()
{
	vector<int> v;

	// set some initial content:
	for (int i = 1; i < 10; i++)
		v.push_back(i);

	v.resize(5);
	v.resize(8, 100);
	v.resize(12);

	cout << "v contains:";
	for (size_t i = 0; i < v.size(); i++)
		cout << ' ' << v[i];
	cout << '\n';
}

// 测试vector的默认扩容机制
// vs:按照1.5倍方式扩容
// linux:按照2倍方式扩容
void TestVectorExpand()
{
	size_t sz;
	vector<int> v;
	sz = v.capacity();
	cout << "making v grow:\n";
	for (int i = 0; i < 100; ++i) 
	{
		v.push_back(i);
		if (sz != v.capacity()) 
		{
			sz = v.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}

// 往vecotr中插入元素时,如果大概已经知道要存放多少个元素
// 可以通过reserve方法提前将容量设置好,避免边插入边扩容效率低
void TestVectorExpandOP()
{
	vector<int> v;
	size_t sz = v.capacity();
	v.reserve(100);   // 提前将容量设置好,可以避免一遍插入一遍扩容
	cout << "making bar grow:\n";
	for (int i = 0; i < 100; ++i) 
	{
		v.push_back(i);
		if (sz != v.capacity())
		{
			sz = v.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}


//  vector的增删改查

// 尾插和尾删:push_back/pop_back
void TestVector4()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);

	auto it = v.begin();
	while (it != v.end()) 
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	v.pop_back();
	v.pop_back();

	it = v.begin();
	while (it != v.end()) 
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

// 任意位置插入:insert和erase,以及查找find
// 注意find不是vector自身提供的方法,是STL提供的算法
void TestVector5()
{
	// 使用列表方式初始化,C++11新语法
	vector<int> v{ 1, 2, 3, 4 };

	// 在指定位置前插入值为val的元素,比如:3之前插入30,如果没有则不插入
	// 1. 先使用find查找3所在位置
	// 注意:vector没有提供find方法,如果要查找只能使用STL提供的全局find
	auto pos = find(v.begin(), v.end(), 3);
	if (pos != v.end())
	{
		// 2. 在pos位置之前插入30
		v.insert(pos, 30);
	}

	vector<int>::iterator it = v.begin();
	while (it != v.end()) 
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	pos = find(v.begin(), v.end(), 3);
	// 删除pos位置的数据
	v.erase(pos);

	it = v.begin();
	while (it != v.end()) {
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

// operator[]+index 和 C++11中vector的新式for+auto的遍历
// vector使用这两种遍历方式是比较便捷的。
void TestVector6()
{
	vector<int> v{ 1, 2, 3, 4 };

	// 通过[]读写第0个位置。
	v[0] = 10;
	cout << v[0] << endl;

	// 1. 使用for+[]小标方式遍历
	for (size_t i = 0; i < v.size(); ++i)
		cout << v[i] << " ";
	cout << endl;

	vector<int> swapv;
	swapv.swap(v);

	cout << "v data:";
	for (size_t i = 0; i < v.size(); ++i)
		cout << v[i] << " ";
	cout << endl;

	// 2. 使用迭代器遍历
	cout << "swapv data:";
	auto it = swapv.begin();
	while (it != swapv.end())
	{
		cout << *it << " ";
		++it;
	}

	// 3. 使用范围for遍历
	for (auto x : v)
		cout << x << " ";
	cout << endl;
}

1.2.2 Use of vector iterator
Use of iterator Interface Description
begin + end (emphasis)
Get the iterator/const_iterator of the first data position , get the iterator/const_iterator of the next position of the last data
rbegin + rend
Get the reverse_iterator of the last data position , and get the reverse_iterator of the previous position of the first data
 1.2.3 Vector space growth problem
capacity space Interface Description
size Get the number of data
capacity Get capacity size
empty Determine whether it is empty
resize (emphasis) Change the size of vector
reserve (emphasis) Change the capacity of vector
  • When the capacity code is run under vs and g++ respectively, you will find that the capacity increases by 1.5 times under vs and 2 times by g++ . This issue is often investigated. Don’t rigidly believe that the vector capacity is increased by 2 times. The specific increase is defined based on specific needs. vs is the PJ version STL , g++ is the SGI version STL .
  • Reserve is only responsible for opening up space. If you know for sure how much space is needed, reserve can alleviate the cost problem of vector expansion.
  • Resize will also initialize while opening space, affecting size .
// 测试vector的默认扩容机制
void TestVectorExpand()
{
    size_t sz;
    vector<int> v;
    sz = v.capacity();
    cout << "making v grow:\n";
    for (int i = 0; i < 100; ++i) 
    {
        v.push_back(i);
        if (sz != v.capacity()) 
        {
            sz = v.capacity();
            cout << "capacity changed: " << sz << '\n';
        }
    }
}

vs:运行结果:vs下使用的STL基本是按照1.5倍方式扩容
making foo grow:
capacity changed: 1
capacity changed: 2
capacity changed: 3
capacity changed: 4
capacity changed: 6
capacity changed: 9
capacity changed: 13
capacity changed: 19
capacity changed: 28
capacity changed: 42
capacity changed: 63
capacity changed: 94
capacity changed: 141

g++运行结果:linux下使用的STL基本是按照2倍方式扩容
making foo grow:
capacity changed: 1
capacity changed: 2
capacity changed: 4
capacity changed: 8
capacity changed: 16
capacity changed: 32
capacity changed: 64
capacity changed: 128
// 如果已经确定vector中要存储元素大概个数,可以提前将空间设置足够
// 就可以避免边插入边扩容导致效率低下的问题了
void TestVectorExpandOP()
{
    vector<int> v;
    size_t sz = v.capacity();
    v.reserve(100); // 提前将容量设置好,可以避免一遍插入一遍扩容
    cout << "making bar grow:\n";
    for (int i = 0; i < 100; ++i) 
    {
        v.push_back(i);
        if (sz != v.capacity())
        {
            sz = v.capacity();
            cout << "capacity changed: " << sz << '\n';
        }
    }
}
1.2.4 Vector addition, deletion, checking and modification
Vector addition, deletion, check and modification Interface Description
push_back (emphasis) tail plug
pop_back (emphasis) tail delete
find Find. (Note that this is an algorithm module implementation, not a member interface of vector )
insert insert val before position
erase Delete position data
swap Swap the data spaces of two vectors
operator[] (emphasis) access like array
1.2.5 Vector iterator failure problem (key points)
        The main function of the iterator is to enable the algorithm to not care about the underlying data structure. The underlying layer is actually a pointer, or it encapsulates the pointer . For example: the iterator of vector is the original pointer T* . Therefore, when the iterator fails, it actually means that the space pointed to by the corresponding pointer at the bottom of the iterator is destroyed, and using a space that has been released will cause the program to crash ( that is, if you continue to use an expired iterator, the program may crash ) .
Operations on vector that may cause its iterator to become invalid include:
  • Operations that will cause the underlying space to change may cause the iterator to fail , such as: resize , reserve , insert , assign , push_back, etc.
#include <iostream>
using namespace std;
#include <vector>
int main()
{
    vector<int> v{1,2,3,4,5,6};
 
    auto it = v.begin();
 
    // 将有效元素个数增加到100个,多出的位置使用8填充,操作期间底层会扩容
    // v.resize(100, 8);
 
    // reserve的作用就是改变扩容大小但不改变有效元素个数,操作期间可能会引起底层容量改变
    // v.reserve(100);
 
    // 插入元素期间,可能会引起扩容,而导致原空间被释放
    // v.insert(v.begin(), 0);
    // v.push_back(8);
 
    // 给vector重新赋值,可能会引起底层容量改变
    v.assign(100, 8);
 
    /*
    出错原因:以上操作,都有可能会导致vector扩容,也就是说vector底层原理旧空间被释放掉,而在打印时,it还使用的是释放之间的旧空间,在对it迭代器操作时,实际操作的是一块已经被释放的空间,而引起代码运行时崩溃。
    解决方式:在以上操作完成之后,如果想要继续通过迭代器操作vector中的元素,只需给it重新赋值即可。
    */
    while(it != v.end())
    {
        cout<< *it << " " ;
        ++it;
    }
    cout<<endl;
    return 0;
}
  • Deletion operation of the element at the specified position - erase
#include <iostream>
using namespace std;
#include <vector>
int main()
{
    int a[] = { 1, 2, 3, 4 };
    vector<int> v(a, a + sizeof(a) / sizeof(int));
    // 使用find查找3所在位置的iterator
    vector<int>::iterator pos = find(v.begin(), v.end(), 3);
    // 删除pos位置的数据,导致pos迭代器失效。
    v.erase(pos);
    cout << *pos << endl; // 此处会导致非法访问
    return 0;
}
        After erase deletes the element at pos position, the elements after pos position will be moved forward, without causing changes in the underlying space. Theoretically, the iterator should not fail, but: if pos happens to be the last element, after deletion, pos happens to be The position of end , and there is no element at the end position, then pos is invalid. Therefore, when deleting an element at any position in the vector , vs considers the iterator at that position to be invalid.
  • Note: Under Linux , the g++ compiler is not very strict in detecting iterator failure, and the processing is not as extreme as vs.
// 1. 扩容之后,迭代器已经失效了,程序虽然可以运行,但是运行结果已经不对了
int main()
{
    vector<int> v{1,2,3,4,5};
    for(size_t i = 0; i < v.size(); ++i)
    cout << v[i] << " ";
    cout << endl;
    auto it = v.begin();
    cout << "扩容之前,vector的容量为: " << v.capacity() << endl;
    // 通过reserve将底层空间设置为100,目的是为了让vector的迭代器失效 
    v.reserve(100);
    cout << "扩容之后,vector的容量为: " << v.capacity() << endl;
 
    // 经过上述reserve之后,it迭代器肯定会失效,在vs下程序就直接崩溃了,但是linux下不会
    // 虽然可能运行,但是输出的结果是不对的
    while(it != v.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
    return 0;
}

/*
程序输出:
1 2 3 4 5
扩容之前,vector的容量为: 5
扩容之后,vector的容量为: 100
0 2 3 4 5 409 1 2 3 4 5
*/

// 2. erase删除任意位置代码后,linux下迭代器并没有失效
// 因为空间还是原来的空间,后序元素往前搬移了,it的位置还是有效的
#include <vector>
#include <algorithm>
int main()
{
    vector<int> v{1,2,3,4,5};
    vector<int>::iterator it = find(v.begin(), v.end(), 3);
    v.erase(it);
    cout << *it << endl;
    while(it != v.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
    return 0;
}

/*
程序可以正常运行,并打印:
4
4 5
*/
 
// 3: erase删除的迭代器如果是最后一个元素,删除之后it已经超过end
// 此时迭代器是无效的,++it导致程序崩溃
int main()
{
    vector<int> v{1,2,3,4,5};
    // vector<int> v{1,2,3,4,5,6};
    auto it = v.begin();
    while(it != v.end())
    {
        if(*it % 2 == 0)
        v.erase(it);
        ++it;
    }
    for(auto e : v)
    cout << e << " ";
    cout << endl;
    return 0;
}

========================================================
// 使用第一组数据时,程序可以运行
[jyq@VM-0-3-centos 20230730]$ g++ testVector.cpp -std=c++11
[jyq@VM-0-3-centos 20230730]$ ./a.out
1 3 5
=========================================================
// 使用第二组数据时,程序最终会崩溃
[jyq@VM-0-3-centos 20230730]$ vim testVector.cpp
[jyq@VM-0-3-centos 20230730]$ g++ testVector.cpp -std=c++11
[jyq@VM-0-3-centos 20230730]$ ./a.out
Segmentation fault
        As can be seen from the above three examples: In SGI STL, after the iterator fails, the code will not necessarily crash, but the running result will definitely be wrong. If it is not within the begin and end range, it will definitely crash.
  • Similar to vector , after string insertion + expansion operation + erase , the iterator will also become invalid.
#include <string>
void TestString()
{
    string s("hello");
    auto it = s.begin();
    // 放开之后代码会崩溃,因为resize到20会string会进行扩容
    // 扩容之后,it指向之前旧空间已经被释放了,该迭代器就失效了
    // 后序打印时,再访问it指向的空间程序就会崩溃
    //s.resize(20, '!');
    while (it != s.end())
    {
        cout << *it;
        ++it;
    }
    cout << endl;
    it = s.begin();
    while (it != s.end())
    {
        it = s.erase(it);
        // 按照下面方式写,运行时程序会崩溃,因为erase(it)之后
        // it位置的迭代器就失效了
        // s.erase(it); 
        ++it;
    }
}
        Solution to iterator failure: Just reassign the iterator before use.

2. In-depth analysis and simulation implementation of vector

2.1 Simulated implementation of the core framework interface of std::vector casso ::vector

Simulation implementation of vector:

#pragma once

#include <iostream>
using namespace std;
#include <assert.h>

namespace casso
{
	template<class T>
	class vector
	{
	public:
		// Vector的迭代器是一个原生指针
		typedef T* iterator;
		typedef const T* const_iterator;

		///
		// 构造和销毁
		vector()
			: _start(nullptr)
			, _finish(nullptr)
			, _endOfStorage(nullptr)
		{}

		vector(size_t n, const T& value = T())
			: _start(nullptr)
			, _finish(nullptr)
			, _endOfStorage(nullptr)
		{
			reserve(n);
			while (n--)
			{
				push_back(value);
			}
		}

		/*
		* 理论上将,提供了vector(size_t n, const T& value = T())之后
		* vector(int n, const T& value = T())就不需要提供了,但是对于:
		* vector<int> v(10, 5);
		* 编译器在编译时,认为T已经被实例化为int,而10和5编译器会默认其为int类型
		* 就不会走vector(size_t n, const T& value = T())这个构造方法,
		* 最终选择的是:vector(InputIterator first, InputIterator last)
		* 因为编译器觉得区间构造两个参数类型一致,因此编译器就会将InputIterator实例化为int
		* 但是10和5根本不是一个区间,编译时就报错了
		* 故需要增加该构造方法
		*/
		vector(int n, const T& value = T())
			: _start(new T[n])
			, _finish(_start+n)
			, _endOfStorage(_finish)
		{
			for (int i = 0; i < n; ++i)
			{
				_start[i] = value;
			}
		}

		// 若使用iterator做迭代器,会导致初始化的迭代器区间[first,last)只能是vector的迭代器
		// 重新声明迭代器,迭代器区间[first,last)可以是任意容器的迭代器
		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		vector(const vector<T>& v)
			: _start(nullptr)
			, _finish(nullptr)
			, _endOfStorage(nullptr)
		{
			reserve(v.capacity());
			iterator it = begin();
			const_iterator vit = v.cbegin();
			while (vit != v.cend())
			{
				*it++ = *vit++;
			}
			_finish = it;
		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _endOfStorage = nullptr;
			}
		}

		/
		// 迭代器相关
		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator cbegin() const
		{
			return _start;
		}

		const_iterator cend() const
		{
			return _finish;
		}

		//
		// 容量相关
		size_t size() const 
		{ 
			return _finish - _start; 
		}

		size_t capacity() const 
		{ 
			return _endOfStorage - _start; 
		}

		bool empty() const 
		{ 
			return _start == _finish; 
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t oldSize = size();
				// 1. 开辟新空间
				T* tmp = new T[n];

				// 2. 拷贝元素
		        // 这里直接使用memcpy会有问题吗?同学们思考下
		        //if (_start)
		        //	memcpy(tmp, _start, sizeof(T)*size);

				if (_start)
				{
					for (size_t i = 0; i < oldSize; ++i)
						tmp[i] = _start[i];

					// 3. 释放旧空间
					delete[] _start;
				}

				_start = tmp;
				_finish = _start + oldSize;
				_endOfStorage = _start + n;
			}
		}

		void resize(size_t n, const T& value = T())
		{
			// 1.如果n小于当前的size,则数据个数缩小到n
			if (n <= size())
			{
				_finish = _start + n;
				return;
			}

			// 2.空间不够则增容
			if (n > capacity())
				reserve(n);

			// 3.将size扩大到n
			iterator it = _finish;
			_finish = _start + n;
			while (it != _finish)
			{
				*it = value;
				++it;
			}
		}

		///
		// 元素访问
		T& operator[](size_t pos) 
		{ 
			assert(pos < size());
			return _start[pos]; 
		}

		const T& operator[](size_t pos)const 
		{ 
			assert(pos < size());
			return _start[pos]; 
		}

		T& front()
		{
			return *_start;
		}

		const T& front()const
		{
			return *_start;
		}

		T& back()
		{
			return *(_finish - 1);
		}

		const T& back()const
		{
			return *(_finish - 1);
		}
		/
		// vector的修改操作
		void push_back(const T& x) 
		{ 
			insert(end(), x); 
		}

		void pop_back() 
		{ 
			erase(end() - 1); 
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endOfStorage, v._endOfStorage);
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos <= _finish);

			// 空间不够先进行增容
			if (_finish == _endOfStorage)
			{
				//size_t size = size();
				size_t newCapacity = (0 == capacity()) ? 1 : capacity() * 2;
				reserve(newCapacity);

				// 如果发生了增容,需要重置pos
				pos = _start + size();
			}

			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				--end;
			}

			*pos = x;
			++_finish;
			return pos;
		}

		// 返回删除数据的下一个数据
		// 方便解决:一边遍历一边删除的迭代器失效问题
		iterator erase(iterator pos)
		{
			// 挪动数据进行删除
			iterator begin = pos + 1;
			while (begin != _finish) {
				*(begin - 1) = *begin;
				++begin;
			}

			--_finish;
			return pos;
		}
	private:
		iterator _start;		// 指向数据块的开始
		iterator _finish;		// 指向有效数据的尾
		iterator _endOfStorage;  // 指向存储容量的尾
	};
}

/// /
/// 对模拟实现的vector进行严格测试
void TestCassoVector1()
{
	casso::vector<int> v1;
	casso::vector<int> v2(10, 5);

	int array[] = { 1,2,3,4,5 };
	casso::vector<int> v3(array, array+sizeof(array)/sizeof(array[0]));

	casso::vector<int> v4(v3);

	for (size_t i = 0; i < v2.size(); ++i)
	{
		cout << v2[i] << " ";
	}
	cout << endl;

	auto it = v3.begin();
	while (it != v3.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	for (auto e : v4)
	{
		cout << e << " ";
	}
	cout << endl;
}

void TestCassoVector2()
{
	casso::vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	cout << v.size() << endl;
	cout << v.capacity() << endl;
	cout << v.front() << endl;
	cout << v.back() << endl;
	cout << v[0] << endl;
	for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	v.pop_back();
	v.pop_back();
	for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	v.insert(v.begin(), 0);
	for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	v.erase(v.begin() + 1);
	for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;
}

2.2 Problems using memcpy to copy

Assuming that the reserve interface in the vector implemented in the simulation is copied using memcpy , what will happen to the following code?
int main()
{
    casso::vector<casso::string> v;
    v.push_back("1111");
    v.push_back("2222");
    v.push_back("3333");
    return 0;
}
problem analysis:
  1. memcpy is a binary format copy of memory, copying the contents of one memory space to another memory space intact;
  2. If you copy a custom type element, memcpy is efficient and error-free. However, if you copy a custom type element, and resource management is involved in the custom type element, an error will occur, because memcpy 's copy is actually Shallow copy. 

 

        Conclusion: If resource management is involved in the object, you must not use memcpy to copy between objects, because memcpy is a shallow copy, otherwise it may cause memory leaks or even program crashes.

2.3  Understanding dynamic two-dimensional arrays

// 以杨慧三角的前n行为例:假设n为5
void test2vector(size_t n)
{
    // 使用vector定义二维数组vv,vv中的每个元素都是
    casso::vector<casso::vector<int>> vv(n);
 
    // 将二维数组每一行中的vecotr<int>中的元素全部设置为1
    for (size_t i = 0; i < n; ++i)
        vv[i].resize(i + 1, 1);
    // 给杨慧三角出第一列和对角线的所有元素赋值
    for (int i = 2; i < n; ++i)
    {
        for (int j = 1; j < i; ++j)
        {
            vv[i][j] = vv[i - 1][j] + vv[i - 1][j - 1];
        }
    }
}
        casso::vector<casso::vector<int>> vv(n); constructs a vv dynamic two-dimensional array. There are a total of n elements in vv . Each element is of vector type, and each row does not contain any elements. If n is 5 , it is as follows:

 After the elements in vv are filled, as shown in the figure below:

When using vector in the standard library to construct a dynamic two-dimensional array, it is actually consistent with the above figure.

Guess you like

Origin blog.csdn.net/weixin_44906102/article/details/132005089