数据结构(四)自定义vector

自定义vector

代码:

//自定义vector实现STL中vector的主要功能,包含自定义,还要加上insert,erase,inverse,find等函数成员
//提示:需要复制粘贴到visual studio 进行测试 


#define VECTOR_H

#include <algorithm>
#include <iostream>
#include <stdexcept>
using namespace std;

template <typename Object>
class Vector
{
	//构造函数和无参构造函数 
public:
	explicit Vector(int initSize = 0)    
		: theSize{ initSize }, theCapacity{ initSize + SPARE_CAPACITY }
	{
		objects = new Object[theCapacity];
	}
//复制构造函数和复制赋值函数 
	Vector(const Vector & rhs)          
		: theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ nullptr }
	{
		objects = new Object[theCapacity];
		for (int k = 0; k < theSize; ++k)
			objects[k] = rhs.objects[k];
	}

	Vector & operator= (const Vector & rhs)
	{
		Vector copy = rhs;
		std::swap(*this, copy);
		return *this;
	}

	~Vector()              
	{
		delete[] objects;
	}
//移动构造函数和移动赋值函数 
	Vector(Vector && rhs)   
		: theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ rhs.objects }
	{
		rhs.objects = nullptr;
		rhs.theSize = 0;
		rhs.theCapacity = 0;
	}

	Vector & operator= (Vector && rhs)
	{
		std::swap(theSize, rhs.theSize);
		std::swap(theCapacity, rhs.theCapacity);
		std::swap(objects, rhs.objects);

		return *this;
	}

	bool empty() const  
	{
		return size() == 0;
	}
	int size() const
	{
		return theSize;
	}
	int capacity() const
	{
		return theCapacity;
	}
//下标运算符重载 
	Object & operator[](int index)
	{
#ifndef NO_CHECK
		if (index < 0 || index >= size())
			//throw ArrayIndexOutOfBoundsException{ };
			cout << "error!" << endl;
#endif
		return objects[index];
	}

	const Object & operator[](int index) const
	{
#ifndef NO_CHECK
		if (index < 0 || index >= size())
			//throw ArrayIndexOutOfBoundsException{ };
			cout << "error!" << endl;
#endif
		return objects[index];
	}

	void resize(int newSize) 
	{
		if (newSize > theCapacity)
			reserve(newSize * 2);
		theSize = newSize;
	}

	void reserve(int newCapacity) 
	{
		if (newCapacity < theSize)
			return;

		Object *newArray = new Object[newCapacity];
		for (int k = 0; k < theSize; ++k)
			newArray[k] = move(objects[k]);

		theCapacity = newCapacity;
		swap(objects, newArray);
		delete[] newArray;
	}
//后方加一个数值
	void push_back(const Object & x)
	{
		if (theSize == theCapacity)
			reserve(2 * theCapacity + 1);
		objects[theSize++] = x;
	}

	void push_back(Object && x)
	{
		if (theSize == theCapacity)
			reserve(2 * theCapacity + 1);
		objects[theSize++] = move(x);
	}
//后方删除一个数值 
	void pop_back()
	{
		if (empty())
			//throw UnderflowException{ };
			cout << "empty" << endl;
		--theSize;
	}

	const Object & back() const
	{
		if (empty())
			//throw UnderflowException{ };
			cout << "error!" << endl;
		return objects[theSize - 1];
	}

	// Iterator stuff: not bounds checked
	typedef Object * iterator;          
	typedef const Object * const_iterator;

	iterator begin()
	{
		return &objects[0];
	}
	const_iterator begin() const
	{
		return &objects[0];
	}
	iterator end()
	{
		return &objects[size()];
	}
	const_iterator end() const
	{
		return &objects[size()];
	}

	iterator insert(iterator p, const Object & x)
	{
		for (iterator i = end(); i != p; i--)
		{
			*i = *(i - 1);
		}
		*p = x;
		theSize = theSize + 1;
		return objects;
	}
	iterator erase(iterator p)
	{
		iterator j = begin();
		for (iterator i = begin(); i<end(); i++)
		{
			if (p != i)
				*(j++) = *i;
		}
		theSize = theSize - 1;
		return objects;
	}
	iterator erase(iterator start, iterator ed) 
	{
		int j = 0;
		iterator t = begin();
		for (iterator i = begin(); i<end(); i++)
		{
			if (i >= start&&i <= ed)
				j++;
			else
				*(t++) = *i;
		}
		theSize = theSize - j;
		return t;
	}
	iterator inverse()  
	{
		int i = 0;
		while (i<theSize - i - 1)
		{
			swap(objects[i], objects[theSize - i - 1]);
			i++;
		}
		return begin();
	}
	iterator find(iterator a, iterator b, const Object& x)
	{
		while (a != b)
		{
			if (*a == x) return a;
			++a;
		}
		return b;
	}
	void print()
	{
		for (int i = 0; i<theSize; i++)
			cout << objects[i] << ' ';
		cout << endl;
	}

	static const int SPARE_CAPACITY = 2;

private:
	int theSize;
	int theCapacity;
	Object * objects;
};
int main() 
{
	Vector<int>a;
	a.push_back(1);
	a.push_back(2);
	a.push_back(3);
	a.push_back(4);
	cout << "容器内的数值是:" << endl;
	a.print();
	
	a.insert(&a[3], 5);
	cout << "在a[3]处插入5:" << endl;
	a.print();
	
	a.erase(&a[4]);
	cout << "在a[4]处删除4:" << endl;
	a.print();
	
	a.erase(&a[4], &a[3]);
	cout << "删除a[4]--a[3]处的元素:" << endl;
	a.print();
	
	a.inverse();
	cout << "倒置:" << endl;
	a.print();
	
	cout << "2所在的位置是:" << endl;
	cout << a.find(a.begin(), a.end(), 2);
	
	system("pause");
	return 0;
}

 

运行:

猜你喜欢

转载自blog.csdn.net/RayMa0305/article/details/81407794