【CPP_STL篇】总结可遍历STL所有容器的迭代器(反向迭代器的模拟+实现)

--------------------------------------------------------------------------------------------------------------------------------

每日鸡汤:火车就是这样离我们很近,又离我们很远,中间像是留有期待,留有着人生注定需要的某种时间。

--------------------------------------------------------------------------------------------------------------------------------

一:STL_迭代器

我们知道STL共有六大组件:

我们已经了解了几个容器和配接器的用法,其中容器就是为了存储数据的,分为数组型,链表型,树形......。

那么存储完数据,是不是就应该访问数据了呢?使得,STL中可使用迭代器访问任意容器的数据。

迭代器:封装封闭底层实现的复杂细节,用统一简单的方式来访问容器。

我们以前在模拟的时候就发现了,string 和 vector的迭代器就是原生指针。

而list的迭代器是自定义类型

下面就根据list容器来"配置"一个我们 自己的反向迭代器。

二:反向迭代器

因为我们当时已经写了一个正向迭代器了,那么就不再需要再重新手搓一个反向的了,只需要复用当时正向迭代器的代码即可。所以目前最大的问题就是如何将反向迭代器与正向迭代器互联起来,形成一个联系,通过这个联系使得反向迭代器复用正向迭代器的代码。想必大家都能够想到,使两个"对象"他们产生联系最好用也是唯一的方法就是使用模板类型实例化,即:

cpp 复制代码
template<class Iterator>
class ReverseIterator
{
public:
    ReverseIterator(Iterator it)    // 将"一个正向的迭代器传至反向迭代器内中,对其进行相关特殊操作,使其具有反向迭代器的独特性质"
        :_it(it)
    {}

    // ...
private:
    Iterator _it;
}

即:就相当于=> 通过正向迭代器去适配反向迭代器。

代码如下:

cpp 复制代码
#pragma once

template<class Iterator, class Ref, class Ptr>
class ReverseIterator
{
public:
	typedef ReverseIterator<Iterator, Ref, Ptr> Self;
	
	ReverseIterator(Iterator it)
		:_it(it)
	{}

	// ++ != *
	Self& operator++()
	{
		--_it;
		return *this;
	}
	Self& operator++(int)
	{
		Self tmp(*this);
		--_it;
		return tmp;
	}

	Self& operator--()
	{
		++_it;
		return *this;
	}
	Self& operator--(int)
	{
		Self tmp(*this);
		++_it;
		return tmp;
	}

	Ref operator*()
	{
		Iterator cur = _it;
		return *(--cur);
	}

	Ptr operator->()
	{
		return &(operator*());
	}

	bool operator!=(const Self& s)
	{
		return _it != s._it;
	}

private:
	Iterator _it;
};

list类内iterator迭代器板块

cpp 复制代码
	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		///
		// Iterators板块
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;

		typedef ReverseIterator<iterator, T&, T*> reverse_iterator;
		typedef ReverseIterator<const_iterator, const T&, const T*> const_reverse_iterator;


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

		reverse_iterator rbegin() { return reverse_iterator(end()); }
		reverse_iterator rend() { return reverse_iterator(begin()); }

		const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
		const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }

        // .........

    }

分析代码:具体式图请移至:CSDN博客汇总/反向迭代器reverse_iterator/CSDN_reverse_iterator/反向迭代器学习板书.png · 阿林/C++代码仓 - 码云 - 开源中国 (gitee.com)

整个list的大概详略图:

三:测试反向迭代器是否适应于每一个容器

3.1:测试string类

cpp 复制代码
	// 测试反向迭代器
	void test_const(const string& st)
	{
		string::const_iterator it = st.begin();
		while (it != st.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		string::const_reverse_iterator rit = st.rbegin();
		while (rit != st.rend())
		{
			cout << *rit << " ";
			rit++;
		}
		cout << endl;
	}

	void test_reverse_iterator()
	{
		string str = "hello world";
		string::iterator it = str.begin();
		while (it != str.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		string::reverse_iterator rit = str.rbegin();
		while (rit != str.rend())
		{
			cout << *rit << " ";
			rit++;
		}
		cout << endl;

		test_const(str);
	}

// 运行结果:
h e l l o   w o r l d
d l r o w   o l l e h
h e l l o   w o r l d
d l r o w   o l l e h

代码式样图:

3.2:测试vector类

cpp 复制代码
	// 测试反向迭代器
	void test_const(const vector<int>& vc)
	{
		vector<int>::const_iterator it = vc.begin();
		while (it != vc.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		vector<int>::const_reverse_iterator rit = vc.rbegin();
		while (rit != vc.rend())
		{
			cout << *rit << " ";
			rit++;
		}
		cout << endl;
	}

	void test_reverse_iterator()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(15);

		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		vector<int>::reverse_iterator rit = v.rbegin();
		while (rit != v.rend())
		{
			cout << *rit << " ";
			rit++;
		}
		cout << endl;

		test_const(v);
	}

// 打印运行结果:
1 2 3 4 15
15 4 3 2 1
1 2 3 4 15
15 4 3 2 1

代码式样图:

3.3:测试list类

cpp 复制代码
	void te(const list<int>& telt)
	{
		list<int>::const_iterator cit = telt.begin();
		while (cit != telt.end())
		{
			cout << *cit << " ";
			++cit;
		}
		cout << endl;

		list<int>::const_reverse_iterator crit = telt.rbegin();
		while (crit != telt.rend())
		{
			cout << *crit << " ";
			++crit;
		}
		cout << endl;
	}	
    
    void test_reverse_iterator()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		list<int>::reverse_iterator rit = lt.rbegin();
		while (rit != lt.rend())
		{
			cout << *rit << " ";
			rit++;
		}
		cout << endl;

		te(lt);
	}

// 运行结果:
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
5 4 3 2 1

代码式样图:

以上所有的代码都存于:CSDN博客汇总/反向迭代器reverse_iterator/CSDN_reverse_iterator · 阿林/C++代码仓 - 码云 - 开源中国 (gitee.com)

希望宝子们可以点一个stars呀。💖💖💖

相关推荐
深圳市恒讯科技1 分钟前
英国服务器Windows系统远程桌面安装与优化
运维·服务器·windows
奶茶树13 分钟前
【C++/STL】map和multimap的使用
开发语言·c++·stl
像风一样的男人@1 小时前
python --两个文件夹文件名比对(yolo 图和label标注比对检查)
windows·python·yolo
diannao7205 小时前
实时将大模型的解决方案转换为随机应变的机器人指令
开发语言·windows·python·机器人
奔跑吧邓邓子5 小时前
【C语言实战(75)】C语言内存探秘:泄漏检测与分析实战
linux·c语言·windows·内存·开发实战·泄露检测
刘恒12345678915 小时前
Windows 10 docker 配置(PHP+Nginx+Mysql)(thinkphp5项目)环境
windows·docker·php
苏比的博客17 小时前
Windows MFC添加类,变量,类导向
c++·windows·mfc
孫治AllenSun21 小时前
【算法】图相关算法和递归
windows·python·算法
TeleostNaCl1 天前
解决 Chrome 无法访问网页但无痕模式下可以访问该网页 的问题
前端·网络·chrome·windows·经验分享
寒某1 天前
在Windows上部署RAGFlow
windows·ai