[C++] Simulated implementation of the vector class (addition, deletion, checking and modification, copy construction, assignment operation, deep and shallow copy)


foreword

We simulate vector to use iterators (start, end, endofstorage) to control addition, deletion, query and modification operations

insert image description here

1. Overall

1. Namespace:

namespace simulation {
    
    
	template<class T>//定义模板
	class vector {
    
    
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		//
		private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};
	}

2 constructors:

1 common structure

vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_endofstorage(nullptr)
		{
    
    }

2 iterator construction

template<class InputIterator>
		//【first,last)左闭右开区间
		vector(InputIterator first, InputIterator last) {
    
    
			while (first != last) {
    
    
				push_back(*first);
				first++;
			}
		}

3 initialization character construction

vector(size_t n, const T& val = T()) {
    
    
//const T& val = T()调用T的默认构造的缺省参数
			resize(n, val);
		}

4 copy construction:

vector(const vector<T>& v) {
    
    
			_start = new T[v.capacity()];
			size_t sz = v.size();
			//提前记录下size
			for (size_t i = 0; i < sz; i++) {
    
    
				_start[i] = v._start[i];//实行深拷贝
			}
			_finish = _start + sz;
			_endofstorage = _start + v.capacity();
		}

3 destructor

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

Two, member function implementation

1. Size

1 current size (size())

//我本身是一个const对象,不可变,所以就需要调用一个const函数,
//但我要是一个非const对象,那么调用非const或者const函数是都都可以的
//这 成员函数加个const,这样const和非const对象都就可以调用了
		size_t size()const    {
    
    
			return _finish - _start;
		}

2 overall capacity (capacity())

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

2. Return the head and tail iterator

1begin()

       iterator begin() {
    
    
			return _start;
		}
		const_iterator begin()const {
    
    
			return _start;
		}

2end()

      iterator end() {
    
    
			return _finish;
		}
		const_iterator end()const {
    
    
			return _finish;
		}

3【】Reference overloading:

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

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

4. Memory reservation (reserve)

	void reserve(size_t n) {
    
    
			if (n > capacity()) {
    
    
				T* tmp = new T[n];
				size_t sz = size();
		//提前存下size,因为后面start会变动
				
				if (_start) {
    
    
					for (size_t i = 0; i < sz; i++) {
    
    
						tmp[i] = _start[i];
					}
//这里拷贝原来的数据不用memcpy是因为memcpy是浅拷贝我们vector要的是深拷贝
//所以用for循环调用赋值运算符重载,实现对象的深拷贝
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_endofstorage = _start + n;
			}
		}

5. Adjust the effective length of the vector (resize)

void resize(size_t n, const T& val = T()) {
    
    
			//将前n个数据初始化为val
			//从当前已有数据后面开始
			if (n < size()) {
    
    
				_finish = _start + n;
				
			}
			else {
    
    
				reserve(n);
				while (_finish != _start+n) {
    
    
					*_finish = val;
					_finish++;
				}
			}
		}

6. Tail plug (push_back)

void push_back(const T& x) {
    
    
			if (_finish == _endofstorage) {
    
    
				//判断是否需要扩容
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}
			*_finish = x;
			_finish++;
			//或者insert(--end());
		}

7. Insert at pos (insert)

iterator insert(iterator pos, const T& x) {
    
    
			assert(pos >= _start && pos <= _finish);
			if (_finish == _endofstorage) {
    
    
				size_t len = pos - _start;
				//算出pos的相对位置
				size_t newcapacity = capacity() == 0 ? 4 : capacity()  
				reserve(newcapacity);
				pos = _start + len;
			}
			iterator end = _finish - 1;
			while (end >= pos) {
    
    
				*(end + 1) = *end;
				--end;
			}
			*pos = x;
			++_finish;
			return pos;
		}

8. Delete pos position (erase)

iterator erase(iterator pos) {
    
    
			assert(pos >= _start && pos < _finish);
			iterator it = pos + 1;
			//将pos后面的数据朝前覆盖
			while (it != _finish) {
    
    
				*(it - 1) = *it;
				++it;
			}
			_finish--;
			return pos;
		}

9. Assignment operator overloading

void swap(vector<T>& v) {
    
    
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}
		vector& operator=(vector<T>  v) {
    
    
			swap(v);
			//创建一个临时对象,临时对象为v的拷贝
			//交换this与v的数据,出了作用域以后
			//this获得新的数据,临时对象v出作用域销毁
			return *this;
		}

Deep copy problem (reserve):

insert image description here

insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/m0_74774759/article/details/131919063