ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++模板技术和STL实战开发(6)——STL概念仿真——容器仿真(Vector和List容器仿真)

2019-02-22 20:51:26  阅读:256  来源: 互联网

标签:仿真 node MyList temp iterator 容器 STL return position


STL容器大多用于输入数据和输出数据的开口,vector是单端开口容器,只能在尾部插入,list是双端开口容器,可以在两端插入

1.vector仿真

#include <iostream>
using namespace std;

//MyVector的类模板
template<typename Ty>
class MyVector
{
public:
	//内嵌类型表:规范它的泛型能力
	typedef Ty value;
	typedef Ty* vec_iter;
public:
	MyVector(int len = 0)
		:m_len(len), mData(NULL), finish(0)
	{
		if (len > 0)
		{
			mData = new Ty[len];
			start = mData;
			end_of_element = len;
		}
	}

	~MyVector()
	{
		delete[] mData;
	}

	void push_back(const value& x)
	{
		if (end_of_element != finish)
		{
			*(start + finish) = x;
			++finish;
		}
		else
		{
			cout << "越界" << endl;
		}
	}
	inline value pop_back() 
	{
		--finish;
		return *(start + finish);
	}

	value& operator[](int n)
	{
		if (n > 0 || n <= finish)
		{
			return *(start + n);
		}
		else
		{
			cout << "取值错误" << endl;
		}
	}
private:
	vec_iter mData; //数组头指针
	int m_len; //数组长度
	vec_iter start; //数组的起始地址
	int finish; //数组的满位标志
	int end_of_element; //数组的末尾标志

};

int main()
{
	int x;
	MyVector<int> vec(10);
	vec.push_back(100);
	vec.push_back(200);
	vec.push_back(300);
	x = vec.pop_back();
	cout << "x = " <<x<< endl;
	cout <<"vec[0] = "<< vec[0] << endl;
	cout << "vec[1] = " << vec[1] << endl;
	cout << "vec[2] = " << vec[2] << endl;
}

测试:

2.List仿真

#include <iostream>
using namespace std;

template <typename T>
struct MyList_node
{
	MyList_node<T>* prev;
	MyList_node<T>* next;
	T data;
};

//将数据类型表和迭代器整合到一起
template <typename T>
struct MyList_iterator
{
	typedef MyList_iterator<T> iterator;
	typedef MyList_node<T>* Link_type;

	Link_type node;
	MyList_iterator(Link_type x)
		:node(x)
	{
		
	}
	MyList_iterator()
		:node(NULL)
	{

	}

	//重载前置++,使得我们可以获得下一个节点的地址
	iterator& operator++()
	{
		node = node->next;
		return *this;
	}

	//重载后置++
	iterator& operator++(int)
	{
		iterator temp = *this;
		++*this;
		//返回未+1前的指针
		return temp;
	}

	//重载前置--
	iterator& operator--()
	{
		node = node->prev;
		return *this;
	}

	//重载后置--
	iterator& operator--(int)
	{
		iterator temp = *this;
		--*this;
		//返回未-1前的指针
		return temp;
	}

	iterator& operator=(iterator x)
	{
		node = x.node;
		return *this;;
	}

	T& operator*()const
	{
		//返回节点的数据域
		return node->data;
	}

	//思考如何重载不等于号,在for循环中使用来用迭代器遍历容器
};


template <typename T>
class MyList
{
public:
	//数据类型表
	typedef MyList_iterator<T> iterator;
protected:
	MyList_node<T>*node; //链表的头指针
	size_t length;//链表长度
public:
	MyList() 
		:length(0)
	{
		node = new MyList_node<T>;
		node->next = node;
		node->prev = node;
	}
	~MyList()
	{}

	//返回链表的头结点的函数
	iterator begin()
	{
		return node->next;
	}

	//返回链表的伪地址
	iterator end()
	{
		return node;
	}

	iterator insert(const iterator& position, const T& x)
	{
		MyList_node<T>* temp = new MyList_node<T>;
		temp->data = x;
		temp->prev = position.node->prev;
		temp->next = position.node;
		position.node->prev->next = temp;
		position.node->prev = temp;
		++length;
		return temp;
	}

	iterator erase(const iterator& position)
	{
		position.node->prev->next = position.node->next;
		position.node->next->prev = position.node->prev;
		--length;
		return position.node;
	}
	//在链表的头部插入元素
	void push_front(const T& x)
	{
		insert(begin(),x);
	}

	//在链表的尾部插入
	void push_back(const T& x)
	{
		insert(end(), x);
	}

	void pop_front()
	{
		erase(begin());
	}

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

int main()
{
	//头部插入测试
	MyList<int>mylist1;
	mylist1.push_front(10);
	mylist1.push_front(20);
	mylist1.push_front(30);

	//迭代器重载的++,*测试 MyList的begin测试
	MyList<int>::iterator iter;
	iter = mylist1.begin();
	cout << *iter << endl;
	cout << *++iter << endl;
	cout << *++iter << endl;
	
	cout << "------------------" << endl;
	////尾部插入测试  迭代器重载的--测试 MyList的end测试
	mylist1.push_back(100);
	mylist1.push_back(200);
	mylist1.push_back(300);
	iter = mylist1.end();
	cout << *--iter << endl;
	cout << *--iter << endl;
	cout << *--iter << endl;

	cout << "------------------" << endl;
	//删除测试
	mylist1.pop_front();
	mylist1.pop_back();
	cout << *mylist1.begin() << endl;
	cout << *--mylist1.end() << endl;
}

测试:

标签:仿真,node,MyList,temp,iterator,容器,STL,return,position
来源: https://blog.csdn.net/qq_34805255/article/details/87872319

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有