【C++】从list模拟实现深入理解iterator

目录

前言

一、list节点类

二、迭代器类

 三、list类


前言

在string与vector这类存储内存连续的容器中,iterator直接使用指针作为底层即可。但对于list这种存储内存不连续的容器,就不可能使用指针作为底层了。

本文我们将通过对list的学习及模拟实现深入了解iterator。


一、list节点类

先创建一个链表节点类。(注:C++中struct和class相似,struct中所有默认为public成员)

//节点类
template<class T>
struct ListNode
{
	ListNode<T>* _prev;
	ListNode<T>* _next;
	T _val;

	ListNode(const T& value = T())
		: _prev(nullptr)
		, _next(nullptr)
		, _val(value)
	{}
};

二、迭代器类

为了方便像指针一样使用迭代器,需要对专门定义一个迭代器类,并对' * '、' ++ '、' -- '、' == '与' != '运算符进行重载。

//迭代器类
template<class T, class Ref, class Ptr>
struct ListIterator
{
	typedef ListNode<T> Node;
	typedef ListIterator<T, Ref, Ptr> Self;

	ListIterator(Node* pNode = nullptr)
		:_pNode(pNode)
	{}

	ListIterator(const Self& l)
	{
		_pNode = l._pNode;
	}

	Ref operator*()
	{
		return _pNode->_val;
	}

	Ptr operator->()
	{
		return &_pNode->_val;
	}

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

	bool operator!=(const Self& l)
	{
		return l._pNode != _pNode;
	}

	bool operator==(const Self& l)
	{
		return l._pNode == _pNode;
	}
	Node* _pNode;
	};

 三、list类

完成了一、二两章的准备工作,我们很简单的就可以写出一个list类。

template<class T>
class list
{
	typedef ListNode<T> Node;
	typedef ListIterator<T, T&, T*> iterator;
	typedef ListIterator<T, const T&, const T*> const_iterator;
public:
	///
	// List的构造
	list()
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
		_size = 0;
	}

	list(int n, const T& value = T())
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
		_size = 0;
		while (n--)
		{
			insert(end(), value);
		}
	}

	template <class Iterator>
	list(Iterator first, Iterator last)
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
		_size = 0;
		while (first != last)
		{
			insert(end(), *first);
			++first;
		}
	}

	list(const list<T>& l)
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
		_size = 0;
		for (auto e : l)
		{
			push_back(e);
		}
	}

	list<T>& operator=(const list<T> l)
	{
		swap(l);
		return *this;
	}

	~list()
	{
		clear();
		delete _head;
		_head = nullptr;
	}


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

	iterator end()
	{
		return _head;
	}

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

	const_iterator end() const
	{
		return _head;
	}


	///
	// List Capacity
	size_t size()const
	{
		return _size;
	}
	bool empty()const
	{
		return _size == 0;
	}


	
	// List Access
	T& front()
	{
		return begin()->_val;
	}

	const T& front()const
	{
		return begin()->_val;
	}

	T& back()
	{
		return end()->_prev->_val;
	}

	const T& back()const
	{
		return end()->_prev->_val;
	}


	
	// List Modify
	void push_back(const T& val) 
	{
		insert(end(), val); 
	}

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

	void push_front(const T& val) 
	{
		insert(begin(), val); 
	}

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

	// 在pos位置前插入值为val的节点
	iterator insert(iterator pos, const T& val)
	{
		Node* node = new Node(val);
		Node* cur = pos._pNode;
		Node* next = cur->_next;
		Node* prev = cur->_prev;
		node->_prev = prev;
		prev->_next = node;
		node->_next = cur;
		cur->_prev = node;
		++_size;
		return pos;
		}

	// 删除pos位置的节点,返回该节点的下一个位置
	iterator erase(iterator pos)
	{
		Node* cur = pos._pNode;
		Node* next = cur->_next;
		Node* prev = cur->_prev;
		delete cur;
		prev->_next = next;
		next->_prev = prev;
		--_size;
		return iterator(next);
	}

	void clear()
	{
		iterator it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}

	void swap(list<T>& l)
	{
		std::swap(_head, l._head);
		std::swap(_size, l._size);
	}

private:
	Node* _head;
	size_t _size;
};

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LRU (Least Recently Used) 是一种常见的页面置换算法,在操作系统中被广泛使用。它的核心思想是,如果一个页面最近一段时间内没有被访问,那么它很可能在未来也不会被访问,因此可以将其替换出内存,腾出空间给其他页面使用。 下面是使用C++模拟LRU算法的示例代码: ```c++ #include <iostream> #include <list> #include <unordered_map> using namespace std; class LRUCache { public: LRUCache(int capacity) : capacity(capacity) {} int get(int key) { auto it = cache.find(key); if (it == cache.end()) { // 如果key不存在 return -1; } // 将节点移动到链表头部,并返回value cache_list.splice(cache_list.begin(), cache_list, it->second); return it->second->second; } void put(int key, int value) { auto it = cache.find(key); if (it != cache.end()) { // 如果key已经存在,更新value并将节点移动到链表头部 it->second->second = value; cache_list.splice(cache_list.begin(), cache_list, it->second); return; } if (cache.size() == capacity) { // 如果cache已满,删除链表尾部节点 auto last = cache_list.back(); cache.erase(last.first); cache_list.pop_back(); } // 在链表头部插入新节点 cache_list.emplace_front(key, value); cache[key] = cache_list.begin(); } private: int capacity; list<pair<int, int>> cache_list; // 使用双向链表保存key-value对 unordered_map<int, list<pair<int, int>>::iterator> cache; // 使用哈希表快速查找key对应的节点 }; int main() { LRUCache cache(2); cache.put(1, 1); cache.put(2, 2); cout << cache.get(1) << endl; // 返回 1 cache.put(3, 3); // 该操作会使得 key 2 作废 cout << cache.get(2) << endl; // 返回 -1 cache.put(4, 4); // 该操作会使得 key 1 作废 cout << cache.get(1) << endl; // 返回 -1 cout << cache.get(3) << endl; // 返回 3 cout << cache.get(4) << endl; // 返回 4 return 0; } ``` 在上面的代码中,我们使用了双向链表和哈希表来维护LRU缓存。其中,双向链表用于按照访问时间排序缓存中的节点,哈希表则用于快速查找某个key对应的节点在链表中的位置。当有新的key-value对要插入缓存时,如果缓存已满,则删除链表尾部的节点;如果key已经存在,则更新value并将对应的节点移动到链表头部;否则,在链表头部插入新节点。当需要从缓存中获取某个key对应的value值时,我们在哈希表中查找key对应的节点,将其移动到链表头部,并返回value。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值