实现C++中的Vector

 

Vector使用模板实现的,里面不仅包含了常用的增删改查,而且包含了迭代器;除了常用的数据类型可以使用之外,自定义的数据类型也可以使用。

一下是代码:

#include<iostream>
#include <stdio.h>
#include<assert.h>
#include <conio.h>
#include<algorithm>
#include<vector>
using namespace std;

template<typename T>
class my_iterator;

template<typename T>
class vec
{
public:
	typedef my_iterator<T> iterator;

	vec()
	{
		//data_ = new T[10];
		data_ = NULL;
		count_ = 0;
		max_size_ = 0;
	}

	~vec()
	{
		count_ = 0;
		max_size_ = 0;
		if (NULL != data_)
		{
			delete []data_;
			data_ = NULL;
		}
	}

	T& operator[](int index)
	{
		return data_[index];
	}
	int Count()
	{
		return count_;
	}

	//返回容器中的元素的个数
	int size()
	{
		return count_;
	}

	//返回容器的容量
	int capacity()
	{
		return max_size_;
	}
	
	//清空容器中的元素
	void clear()
	{
		count_ = 0;
	}

	//判断是否为空,为空则返回true,否则返回false
	bool empty()
	{
		/*if (count_ == 0) 
		{
			return true;
		}
		else
		{
			return false;
		}*/
		return count_ == 0;
	}

	//插入元素
	void insert(int index, T& data)
	{
		
		/*if (index <= -1 && index >= count_)
		{
			throw exception("插入的位置不符合要求!");
		}*/

		if (index >= 0 && index <= count_)
		{

			if (count_ >= max_size_)
			{
				//扩容,第一次的话就扩容1个,后面扩容的话扩大为前面的二倍
				reserve((count_ > 0) ? count_ * 2 : 1);
			}
			if (index < count_)
			{
				//将index之后的元素整体向后移动一个单位,在index位置插入data
				memmove(data_ + index + 1, data_ + index, sizeof(T)*(count_ - index));
			}
			data_[index] = data;
			count_++;
		}
		else
		{
			throw exception("插入的位置不符合要求!");
		}
	}
	//删除元素
	void Delete(int index)
	{
		if (index <= -1 && index > count_)
		{
			throw exception("删除的位置有不符合要求");
		}
		//将index以后的元素整体向前移动一个单位
		memmove(data_+index-1,data_+index,sizeof(T)*(count_-index));
		count_--;
	}
	//扩容  
	void reserve(int count)
	{
		if (count > count_&&count != max_size_)
		{
			T* p = new T[count];
			
			for (int i = 0; i < count_; i++)
			{
				p[i] = data_[i];
			}	

			if (data_ != NULL)
			{
				delete []data_;	
			}
			data_ = p;
			max_size_ = count;
		}
	}
	//扩容
	void resize(int newsize)
	{
		if (newsize < 0)
		{
			throw exception("扩容的大小不符合要求!");
		}
		if (newsize > count_)
		{
			reserve(newsize);
		}
		else if(newsize >= 0)
		{
			count_ = newsize;
		}
	}

	//尾插
	void push_back(T& data)
	{
		/*if (count_ >= max_size_)
		{
			reserve(count_);
		}*/
		insert(count_, data);
	}
	//尾删
	void pop_back(T& data)
	{
		if (count_ > 0)
		{
			Delete(count_);
		}
	}
	//输出
	void print()
	{
		for (int i = 0; i < count_; i++)
		{
			cout << data_[i] << " ";
		}
		cout << endl;
	}

	//返回容器中的第一个元素
	T front()
	{
		if (empty())
		{
			throw exception("此时的容器为空!");
		}
		return data_[0];
	}

	//返回容器中的最后一个元素
	T back()
	{
		if (empty())
		{
			throw exception("此时的容器为空!");
		}
		return data_[count_ - 1];
	}

	//返回第i个元素
	T at(int i)
	{
		if (i < 1 && i > count_)
		{
			throw exception("输入的值不符合要求");
		}
		return data_[i - 1];
	}
	
	//迭代器begin
	iterator begin()
	{
		return iterator(this, 0);
	}

	//迭代器end
	iterator end()
	{
		return iterator(this, count_);
	}
	
private:
	
	T* data_;
	int count_;
	int max_size_;
};


template<typename T>
class vec;
template<typename T>
class my_iterator
{
public:
	//构造
	
	my_iterator(vec<T>* v,int ind)
		:vec_(v), index_(ind)
	{
	}
	
	bool operator!=(const my_iterator left)
	{ 
		return index_ != left.index_;	
	}


	my_iterator& operator++()
	{
		index_++;
		return *this;
	}

	const my_iterator operator++(int)
	{
		my_iterator<T> tmp(*this);
		index_++;
		return *this;
	}

	my_iterator& operator--()
	{
		index_--;
		return *this;
	}
	const my_iterator operator--(int)
	{
		my_iterator<T> tmp(*this);
		index_--;
		return *this;
	}

	const my_iterator operator+(int a)
	{
		my_iterator<T> tmp(*this);
		tmp.index_ += a;
		return tmp;
	}

	const my_iterator operator-(int a)
	{
		my_iterator<T> tmp(*this);
		tmp.index_ -= a;
		return tmp;
	}

	T& operator*();
	friend class vec<T>;
private:
	vec<T>* vec_;
	int index_;
};
template<typename T>
T& my_iterator<T>::operator*()
{
	return (*vec_)[index_];
}


struct stu {
	
	int num_;

	stu& operator=(stu &s)
	{
		num_ = s.num_;
		return *this;
	}

	stu& operator+(stu &s)
	{
		num_ += s.num_;
		return *this;
	}

	stu& operator-(stu& s)
	{
		num_ -= s.num_;
		return *this;
	}

	friend ostream &operator<<(ostream &out, stu s);
};

ostream& operator<<(ostream &out, stu s)
{
	cout << s.num_ << " ";
	return out;
}

一下是测试代码

/*
	vec<int> v;
	
	for (int i = 0; i < 10; i++)
	{
		v.insert(0, i);
	}
	cout << "头插结果为:";
	v.print();
	cout << "当前元素的个数:" << v.size() << endl;
	cout << "容器的容量:" << v.capacity() << endl;
	cout << "第一个元素是:" << v.front() << endl;
	cout << "最后一个元素:" << v.back() << endl;
	cout << "第1个元素是:" << v.at(1) << endl;
	cout << "第2个元素是:" << v.at(2) << endl;
	cout << endl;
	
	v.Delete(10);
	cout << "尾删1次后结果:";
	v.print();
	cout << "当前元素的个数:" << v.size() << endl;
	cout << "容器的容量:" << v.capacity() << endl;
	cout << "第一个元素是:" << v.front() << endl;
	cout << "最后一个元素:" << v.back() << endl;
	cout << "第1个元素是:" << v.at(1) << endl;
	cout << "第2个元素是:" << v.at(2) << endl;
	cout << endl;
	
	v.Delete(9);
	v.Delete(8);
	v.Delete(7);
	cout << "尾删3次后结果:";
	v.print();
	cout << "当前元素的个数:" << v.size() << endl;
	cout << "容器的容量:" << v.capacity() << endl;
	cout << "第一个元素是:" << v.front() << endl;
	cout << "最后一个元素:" << v.back() << endl;
	cout << "第1个元素是:" << v.at(1) << endl;
	cout << "第2个元素是:" << v.at(2) << endl;
	cout << endl;
	*/
	
	/*
	vec<int> v1;
	cout << "尾插结果为:";
	for (int i= 0; i <10; i++)
	{
		v1.push_back(i);
	}
	v1.print();
	cout << "当前元素的个数:" << v1.size() << endl;
	cout << "容器的容量:" << v1.capacity() << endl;
	cout << "第一个元素是:" << v1.front() << endl;
	cout << "最后一个元素:" << v1.back() << endl;
	cout << "第1个元素是:" << v1.at(1) << endl;
	cout << "第2个元素是:" << v1.at(2) << endl;
	cout << endl;

	for (int i = 0; i < 3; i++)
	{
		v1.pop_back(i);
	}
	cout << "尾删三次后的结果:";
	v1.print();
	cout << "当前元素的个数:" << v1.size() << endl;
	cout << "容器的容量:" << v1.capacity() << endl;
	cout << "第一个元素是:" << v1.front() << endl;
	cout << "最后一个元素:" << v1.back() << endl;
	cout << "第1个元素是:" << v1.at(1) << endl;
	cout << "第2个元素是:" << v1.at(2) << endl;
	cout << "此时容器";
	if (v1.empty())
	{
		cout << "为空!" << endl;
	}
	else
	{
		cout << "不为空!" << endl;
	}
	cout << endl;

	

	cout << "清除容器后:";
	v1.clear();
	cout << "当前元素的个数:" << v1.size() << endl;
	cout << "容器的容量:" << v1.capacity() << endl;
	cout << "此时容器";
	if (v1.empty())
	{
		cout << "为空!" << endl;
	}
	else
	{
		cout << "不为空!" << endl;
	}
	cout << endl;

	cout << "容器扩容50个大小后:";
	v1.resize(50);
	cout << "当前元素的个数:" << v1.size() << endl;
cout << "容器的容量:" << v1.capacity() << endl;
	cout << "此时容器";
	if (v1.empty())
	{
		cout << "为空!" << endl;
	}
	else
	{
		cout << "不为空!" << endl;
	}
	*/
	

/*
	vec<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v2.insert(0, i);
	}
	cout << "当前容器中的元素为:";
	v2.print();
	vec<int>::iterator it = v2.begin();
	cout <<"当前容器的begin的值为:"<< *it << endl;
	cout << "begin的前置++的值:" <<  (*it)++ << endl;
	cout << "begin的后置++的值:" << ++(*it) << endl;
	cout <<"begin-1的值:" <<*it - 1 << endl;
	cout << "begin+1的值:"<<*it + 1 << endl;
	
	
	
	cout << "通过迭代器打印为:";
	for (it; it != v2.end(); it++)
	{
		cout << *it <<" ";
	}
	cout << endl;
	cout << (*it != ((*it)++)) << endl;
	cout << (*it !=( ++(*it))) << endl;
	*/
	
   /*
	try {
		vec<int> v2;
		for (int i = 0; i < 10; i++)
		{
			v2.insert(0, i);
		}
		cout << "当前容器中的元素为:";
		v2.print();

		cout << "扩容后为0后" << endl;
		v2.resize(0);
		cout << "当前元素的个数:" << v2.size() << endl;
		cout << "容器的容量:" << v2.capacity() << endl;

		cout << "扩容后为-1后" << endl;
		v2.resize(-1);
		cout << "当前元素的个数:" << v2.size() << endl;
		cout << "容器的容量:" << v2.capacity() << endl;
	}
	catch (exception& e) {
		cout << e.what() << endl;
	}
	*/


/*
	try {
		vec<char> v1;
		char a[] = "1234567";
		for (int i = 0; i < sizeof(a) / sizeof(char) - 1; i++)
		{
			v1.push_back(a[i]);
		}
		cout << "当前容器中的元素:";
		v1.print();
		cout << "当前元素的个数:" << v1.size() << endl;
		cout << "容器的容量:" << v1.capacity() << endl;
		cout << "第一个元素是:" << v1.front() << endl;
		cout << "最后一个元素:" << v1.back() << endl;
		cout << "第1个元素是:" << v1.at(1) << endl;
		cout << "第2个元素是:" << v1.at(2) << endl;
		cout << endl;

		if (v1.empty())
		{
			cout << "为空!" << endl;
		}
		else
		{
			cout << "不为空!" << endl;
		}

		vec<char>::iterator it = v1.begin();
		cout << "当前容器的begin的值为:" << *it << endl;
		cout << "begin的前置++的值:" << (*it)++ << endl;
		cout << "begin的后置++的值:" << ++(*it) << endl;
		cout << "begin-1的值:" << (*it - 1)-'0' << endl;
		cout << "begin+1的值:" << (*it + 1)-'0' << endl;


		it = v1.begin();
		cout << "通过迭代器打印为:";
		for (it; it != v1.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
		cout << (*it != ((*it)++)) << endl;
		cout << (*it != (++(*it))) << endl;
		cout << endl;
		v1.clear();
		cout << "清空后的元素数量:" << v1.size() << endl;
		cout << "清空后总容量:" << v1.capacity() << endl;
	}
	catch (exception& e) {
		cout << e.what() << endl;
	}
*/

/*
	try {
		vec<stu> v1;
		stu s1, s2, s3;


		s1.num_ = 1;
		//v1.insert(4, s1);
		v1.push_back(s1);
		s2.num_ = 5;
		v1.push_back(s2);
		s3.num_ = 10;
		v1.push_back(s3);

		cout << "容器中的元素:";
		for (int i = 0; i < 3; i++)
		{
			cout << v1[i];
		}
		cout << endl;
		cout << "结构体1+结构体2:";
		cout << s1 + s2 << endl;
		cout << "结构体2+结构体3:";
		cout << s2 + s3 << endl;
		cout << "此时容器中的元素:" << s1 << " " << s2 << " " << s3 << endl;
		cout << "结构体1-结构体2:";
		cout << s1 - s2 << endl;
		cout << "此时容器中的元素:" << s1 << " " << s2 << " " << s3 << endl;
		cout << "结构体3-结构体2:";
		cout << s3 - s2 << endl;
		cout << "此时容器中的元素:" << s1 << " " << s2 << " " << s3 << endl;
		cout << "将结构体3赋值给结构体1:" << (s1 = s3) << endl;
		cout << "此时容器中的元素:" << s1 << " " << s2 << " " << s3 << endl;
		cout << "当前元素的个数:" << v1.size() << endl;
		cout << "容器的容量:" << v1.capacity() << endl;
		cout << "第一个元素是:" << v1.front() << endl;
		cout << "最后一个元素:" << v1.back() << endl;
		cout << "第1个元素是:" << v1.at(1) << endl;
		cout << "第2个元素是:" << v1.at(2) << endl;
		cout << endl;

		if (v1.empty())
		{
			cout << "为空!" << endl;
		}
		else
		{
			cout << "不为空!" << endl;
		}

		std::cout << "123" << endl;
		cout << '1' << endl;
	}
	catch (exception& e)
	{
		cout << e.what() << endl;
	}

	*/
	vec<int> v2;
	int a = 4;
	v2.push_back(a);
	a = 8;
	v2.push_back(a);
	a = 1;
	v2.push_back(a);
	a = 3;
	v2.push_back(a);
	a = 6;
	v2.push_back(a);
	a = 20;
	v2.push_back(a);
	a = 17;
	v2.push_back(a);
	vec<int>::iterator it = v2.begin();
	//v2.print();
	for (it; it != v2.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值