C++模板与STL(二):STL概念仿真

目录

1.Vector容器仿真

1.1 定义内嵌类型表

1.2 定义相关属性

1.3 push_back()

1.4 back() 

1.5 下标操作[]

1.6 总体代码与测试案例

2.List仿真

2.1 定义结点

2.2 设计迭代器(将类型表与迭代器整合)

2.2.1 重载++

2.2.2 重载--

2.2.3 重载=

2.2.4 重载*(返回结点数据)

2.2.5 迭代器总体代码:

2.3 设计MyList

2.3.1 数据类型表:将MyList_iterator声明为内部类型Iterator

2.3.2  构造函数初始化

2.3.3  设计begin()/end()

2.3.4 设计插入元素 insert()

2.3.5 设计删除 erase()

2.3.6 基于insert()、erase()设计push_back()/push_front()/pop_back()/pop_front()

2.3.7 MyList整体代码

2.4 测试案例


1.Vector容器仿真

1.1 定义内嵌类型表

public:
	//内嵌类型表
	typedef T value;
	typedef T* vIter;

1.2 定义相关属性

protected:
	vIter m_Data;//数组头指针
	int m_nLen;//数组长度
	vIter start;//数组的起始地址
	int finish;//数组的满位标志
	int end_of_element;//数组的末尾标志

1.3 push_back()

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

1.4 back() 

	inline value back() {
		--finish;
		return *(start + finish);
	}

1.5 下标操作[]

	value operator[](int n) {
		if (n <= finish) {
			return *(start + n);
		}
		else {
			cout << "取值错误<" << endl;
		}
	}

1.6 总体代码与测试案例

#include <iostream>
using namespace std;

//MyVector的类模板
template<typename T>
class MyVector {
public:
	//内嵌类型表
	typedef T value;
	typedef T* vIter;

public:
	MyVector(int nLen = 0) :m_nLen(nLen), m_Data(nullptr), finish(0) {
		if (nLen > 0) {
			m_Data = new T[nLen];
			start = m_Data;
			end_of_element = nLen;
		}
	}
	~MyVector()
	{

	}

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

	inline value back() {
		--finish;
		return *(start + finish);
	}

	value operator[](int n) {
		if (n <= finish) {
			return *(start + n);
		}
		else {
			cout << "取值错误<" << endl;
		}
	}

protected:
	vIter m_Data;//数组头指针
	int m_nLen;//数组长度
	vIter start;//数组的起始地址
	int finish;//数组的满位标志
	int end_of_element;//数组的末尾标志
};

int main() {
	int x;
	MyVector<int>v(10);
	v.push_back(100);
	v.push_back(200);
	v.push_back(300);
	x = v.back();
	cout << "x= " << x << endl;
	cout << v[0] << endl;
	cout << v[1] << endl;
	cout << v[2] << endl;
	//cout << v[3] << endl;
	return 0;
}

2.List仿真

2.1 定义结点

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

2.2 设计迭代器(将类型表与迭代器整合)

//同时将设计类型表和迭代器整合在一起

//迭代器
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(nullptr) {

	}

2.2.1 重载++

	//重载++,使得我们可以获得下个结点的地址
	iterator& operator++() {
		node = node->next;
		//返回本对象,同时指向下一个
		return *this;
	}
	iterator& operator++(int) {
		//保存自增运算前的指针值
		iterator temp = *this;
		//本对象加一
		++* this;
		return temp;
	}

2.2.2 重载--

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

	iterator& operator--(int) {
		iterator temp = *this;
		--* this;
		return temp;
	}

2.2.3 重载=

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

2.2.4 重载*(返回结点数据)

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

2.2.5 迭代器总体代码:

//迭代器
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(nullptr) {

	}

	//重载++,使得我们可以获得下个结点的地址
	iterator& operator++() {
		node = node->next;
		//返回本对象,同时指向下一个
		return *this;
	}
	iterator& operator++(int) {
		//保存自增运算前的指针值
		iterator temp = *this;
		//本对象加一
		++* this;
		return temp;
	}
	//重载--
	iterator& operator--() {
		node = (node)->prev;
		return *this;
	}

	iterator& operator--(int) {
		iterator temp = *this;
		--* this;
		return temp;
	}

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

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

2.3 设计MyList

2.3.1 数据类型表:将MyList_iterator声明为内部类型Iterator

//MyList
template<typename T>
class MyList {
public:
	//数据类型表
	//将MyList_iterator<T>声明为内部类型Iterator
	typedef MyList_iterator<T> iterator;

protected:
	MyList_node<T>* node;
	size_t length;

2.3.2  构造函数初始化

	MyList() :length(0) {
		node = new MyList_node<T>;
		node->next = node;
		node->prev = node;
	}
	~MyList(){

	}

2.3.3  设计begin()/end()

	//返回链表的头函数
	iterator begin() {
		return node->next;
	}
	//返回链表尾地址
	iterator end() {
		return node;
	}

2.3.4 设计插入元素 insert()

	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;
	}

2.3.5 设计删除 erase()

	void erase(const iterator& position) {
		position.node->prev->next = position.node->next;
		position.node->next->prev = position.node->prev;
		--length;
	}

2.3.6 基于insert()、erase()设计push_back()/push_front()/pop_back()/pop_front()

	//push_front()
	void push_front(const T& x) {
		insert(begin(), x);
	}
	
	//push_back()
	void push_back(const T& x) {
		insert(end(), x);
	}

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

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

2.3.7 MyList整体代码

//MyList
template<typename T>
class MyList {
public:
	//数据类型表
	//将MyList_iterator<T>声明为内部类型Iterator
	typedef MyList_iterator<T> iterator;
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;
	}

	//push_front()
	void push_front(const T& x) {
		insert(begin(), x);
	}
	
	//push_back()
	void push_back(const T& x) {
		insert(end(), x);
	}

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

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

	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;
	}

	void erase(const iterator& position) {
		position.node->prev->next = position.node->next;
		position.node->next->prev = position.node->prev;
		--length;
	}

protected:
	MyList_node<T>* node;
	size_t length;
};

2.4 测试案例

int main() {
	MyList<int>myList1;
	myList1.push_front(10);
	myList1.push_front(20);
	myList1.push_front(30);

	MyList<int>::iterator iter;
	iter = myList1.begin();
	cout << *iter << endl;
	cout << *++iter << endl;
	cout << *++iter << endl;
	cout << endl;

	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;

	return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值