IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++ string vector list迭代器设计 -> 正文阅读

[C++知识库]C++ string vector list迭代器设计

list的结构不像vector和string
vector和string其储存数据的空间是连续的,list储存数据的空间是随机的。

vector和string的迭代器本质上是指针,其可以支持++ – *等操作。

为了让list也可以支持类似指针的行为,所以对list的迭代器进行了封装

先看string的迭代器:

string的迭代器本质上是字符指针,const迭代器同理为const指针

要注意在string构造函数时要多开辟一个空间存放’ \0’

#include<iostream>

using namespace std;

namespace My
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin()
		{
			return _src;
		}
		iterator end()
		{
			return _src + size;
		}
		const_iterator begin()const
		{
			return _src;
		}
		const_iterator end()const
		{
			return _src + size;
		}
		string(const char*src="")
			:_src(new char[strlen(src)+1])
		{//多开辟一个空间存放'\0'
			strcpy(_src, src);
			size = strlen(_src);
			capacity = size;
		}
		~string()
		{
			delete[]_src;
			_src = nullptr;
			size = 0;
			capacity = 0;
		}
	private:
		char*_src;//指向字符串
		int size;//字符串的大小
		int capacity;//字符串的容量
	};
}

在利用迭代器遍历string

int main()
{
	My::string s1 = "abcde";
	My::string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	for (auto e : s1)
	{
		cout << e << " ";
	}
}

在这里插入图片描述

vector的迭代器

#include<iostream>

using namespace std;

namespace My
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;//const迭代器
		vector()
			: _start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{}

		~vector()
		{
			if (_start != nullptr)
			{
				delete[]_start;
			}
			_start = _finish = _endofstorage = 0;
		}

		//迭代器
		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin()const
		{
			return _start;
		}

		const_iterator end()const
		{
			return _finish;
		}

		void reserve(int n)
		{
			if (n > capcity())
			{
				T* tmp = new T[n];
				int sz = size();
				if (_start)//原数据不是空,需要拷贝数据
				{
					memcpy(tmp, _start,sz*sizeof(T));//这里不考虑自定义类型。
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_endofstorage = _start + n;
			}
		}

		int capcity()
		{
			return _endofstorage - _start;
		}

		int size()
		{
			return _finish - _start;
		}

		void push_back(const T&e)
		{
			if (_finish == _endofstorage)
			{
				int newcapcity = capcity() == 0 ? 5 : 2 * capcity();
				reserve(newcapcity);//增容
			}
			*_finish = e;
			_finish++;
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	//vector的大小为_finish-_start;
	//vector的容量为_endofstorage-_start
	};
}
int main()
{
	My::vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	My::vector<int>::iterator it = v1.begin();
	while (it != v1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	for (auto e : v1)
	{
		cout << e << " ";
	}

}

在这里插入图片描述

list的迭代器因为其储存方式不连续需要对迭代器进行封装

首先list结构的基本形式为

#include<iostream>

using namespace std;

namespace My
{
	template<class T>
	struct _List_Node//链表节点
	{
		T _val;
		struct _List_Node*_next;
		struct _List_Node*_prev;
		_List_Node(const T&val = T())
			:_val(val), _next(nullptr), _prev(nullptr)
		{}
	};

	template <class T>
	class list
	{
	public:
		typedef _List_Node<T> node;

		list()//构造函数
		{
			_head = new node;//调用模板函数的默认构造函数
			_head->_next = _head;//双向带头链表
			_head->_prev = _head;
		}

	private:
		node*_head;
	};
}

在此基础上,我们定义迭代器类:
有两种情况,一种为普通迭代器,另一种为const修饰的迭代器,我们这里使用模板来实例化两个迭代器

template<class T,class Ref>
	struct List_iterator//迭代器类
	{

	};

	template <class T>
	class list
	{
	public:
		typedef _List_Node<T> node;
		typedef List_iterator<T, T&> iterator;
		typedef List_iterator<T, const T&> const_iterator;
		list()//构造函数
		{
			_head = new node;//调用模板函数的默认构造函数
			_head->_next = _head;//双向带头链表
			_head->_prev = _head;
		}

	private:
		node*_head;
	};

list迭代器++相当于走向list的下一个节点, *是指通过节点指针来访问该节点的值等等

#include<iostream>

using namespace std;

namespace My
{
	template<class T>
	struct _List_Node//链表节点
	{
		T _val;
		struct _List_Node*_next;
		struct _List_Node*_prev;
		_List_Node(const T&val = T())
			:_val(val), _next(nullptr), _prev(nullptr)
		{}
	};

	template<class T,class Ref>
	struct List_iterator//迭代器类
	{
		typedef _List_Node<T> node;
		typedef List_iterator<T, Ref> Self;
		node* _pnode;

		List_iterator(node*pnode)
			:_pnode(pnode)
		{}

		Ref& operator * ()//const对象Ref为const &,非const对象返回&
		{
			return _pnode->_val;
		}

		bool operator !=(const Self&s)const //不修改
		{
			return _pnode != s._pnode;//判断两个节点的地址是否相同
		}

		Self& operator ++()//前置++
		{
			_pnode = _pnode->_next;
			return *this;
		}

		Self operator++(int)//后置++
		{
			Self tmp = *this;//构造函数
			_pnode = _pnode->_next;
			return tmp;
		}

		Self& operator --()//前置--
		{
			_pnode = _pnode->_prev;
			return *this;
		}

		Self operator --(int)//后置--
		{
			Self tmp = *this;
			_pnode = _pnode->_prev;
			return tmp;
		}
	};

	template <class T>
	class list
	{
	public:
		typedef _List_Node<T> node;
		typedef List_iterator<T, T&> iterator;
		typedef List_iterator<T, const T&> const_iterator;
		list()//构造函数
		{
			_head = new node;//调用模板函数的默认构造函数
			_head->_next = _head;//双向带头链表
			_head->_prev = _head;
		}

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end() const
		{
			return const_iterator(_head);
		}

		void push_back(const T&x)
		{
			node* NewNode = new node(x);
			node*tail = _head->_prev;
			tail->_next = NewNode;
			_head->_prev = NewNode;
			NewNode->_prev = tail;
			NewNode->_next = _head;
		}

	private:
		node*_head;
	};
}

const迭代器Ref为const T&这样通过模板,相当于构造了两个迭代器。

int main()
{
	My::list<int>v1;
	v1.push_back(1);
	v1.push_back(2);
	My::list<int>::iterator it = v1.begin();
	while (it != v1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	for (auto e : v1)
	{
		cout << e << " ";
	}
	return 0;
}

在这里插入图片描述

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-14 13:50:50  更:2021-08-14 13:52:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 7:41:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码