STL learning - Analog Talking vector class implementation

The document describes the vector

vector Description:

  1. vector is a sequence of variable-sized array of the container.
  2. Like arrays, vector also uses continuous storage space to store elements. That is meant to be an element vector using index
    access, and array as efficient. However, unlike the array, its size can be dynamically changed, and its size will be automatically processed container.
  3. Nature speaks, vector dynamically allocated array to store its elements. When a new element is inserted when the array needs to be reallocated in order to increase the size of storage space. Its approach is to allocate a new array, and then move all the elements of the array. This is the constructor declaration
    Vector () constructor with no arguments
    vector (size_type n, const value_type & val = value_type ()) constructs and initializes n-Val
    Vector (const Vector & X); copy configuration
    vector (InputIterator first, InputIterator last) ; iterative to initialize a structure is relatively costly task, because every time a new element is added to the container when, vector and does not reallocate each size.
  4. vector space allocation strategy: vector will allocate some extra space to accommodate the growth potential, because the storage space is larger than the actual storage space required. Using different libraries use different strategies and trade-offs of space reallocation. But in any case, should be reallocated logarithmic growth interval size, so that in the end to insert an element of time is the time constant of the complex completed.
  5. Therefore, vector takes up more storage space, in order to obtain the ability to manage storage space, and in an efficient way to grow dynamically.
  6. Compared with other dynamic sequence containers (deques, lists and forward_lists), vector elements when accessing more efficient, relatively efficient to add and delete elements in the end. For the other end of the insertion and deletion operation is not less efficient. Compared to lists and forward_lists unified iterators and references better.

Vector analog implementation

Here Insert Picture Description
Member variables:

//typedef T* Iterator

private:
	Iterator _start;
	Iterator _finish;
	Iterator _endofstorage;
namespace tonglin{

	template<class T>

class Vector{
public:
	//vector的迭代器是一个原生指针
	typedef T* Iterator;
	typedef const T*  ConstIterator;
	Iterator Begin() { return _start; }
	Iterator End() { return _finish; }
	ConstIterator CBegin() const { return _start; }
	ConstIterator CEnd() const { return _finish; }
	size_t Size() const { return _finish - _start; }
	size_t Capacity() const { return _endOfStorage - _start; }

	Vector()
		: _start(nullptr)
		, _finish(nullptr)
		, _endOfStorage(nullptr)
	{}

	Vector(int n, const T& value = T())
		: _start(nullptr)
		, _finish(nullptr)
		, _endOfStorage(nullptr)
	{
		Reserve(n);
		while (n--)
		{
			PushBack(value);
		}
	}

	// 这里如果使用Iterator做迭代器,就会导致初始化的迭代器区间[first,last]
	//只能是Vector的迭代器
	// 这里重新声明迭代器,迭代器区间[first,last]可以是任意容器的迭代器区间。
	template<class InputIterator>
	Vector(InputIterator first, InputIterator last)
	{
		Reserve(last - first);
		while (first != last)
		{
			PushBack(*first);
			++first;
		}
	}

	///////capacity////////////
	void resize(size_t n, contst 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;
		Iterator _finish = _start + n;
		while (it != _finish)
		{
			*it = value;
			++it;
		}
	}

	void reserve(size_t n){
	
		if (n > Capacity())
		{
			size_t size = Size();
			T* tmp = new T[n];
			// 这里直接使用memcpy是有问题的
			// 以后我们会用更好的方法解决
			//if (_start)
			// memcpy(tmp, _start, sizeof(T)*size);
			if (_start)
			{
				for (size_t i = 0; i < size; ++i)
					tmp[i] = _start[i];
			}
			
			delete[]_start;
			_start = tmp;
			_finish = _start + size;
			_endOfStorage = _start + n;
		}
	
	}

	void Swap(Vector<T>& v)
	{
		swap(_start, v._start);
		swap(_finish, v._finish);
		swap(_endOfStorage, v._endOfStorage);
	}



	void PushBack(const T& x)
	{
		Insert(End(), x);
	}
	void PopBack()
	{
		Erase(--End());
	}

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

	Iterator Insert(Iterator pos,T& x){
	
		//在pos位置插入元素x
	
		assert(pos <= _finish);   //参数合法检测

		
		if (finish == endOfStorage){
		//需要增容
			size_t size = Size();
			size_t newcapacity = Capacity() == 0 
			   ? 3 : 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){
	   //删除pos位置的元素

		// 挪动数据进行删除
		Iterator begin = pos + 1;
		while (begin != _finish) {
			*(begin - 1) = *begin;
			++begin;
		}

		--_finish;
		return pos;

	}

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



private:
		Iterator _start;
		Iterator _finish;
		Iterator _endOfStorage

	};



};


Guess you like

Origin blog.csdn.net/tonglin12138/article/details/92844875
Recommended