STL的list模拟实现(带移动构造和emplace版本)

  • list.h
c 复制代码
#pragma once
#include<iostream>
#include<assert.h>
#include<algorithm>
#include"ReverseIterator.h"
#include<initializer_list>
#include <utility>

using namespace std;

//双向带头循环链表

namespace bit
{
	template<class T>
	struct ListNode
	{
		ListNode* _next;
		ListNode* _prev;
		T _data;

		ListNode(const T& x=T())
			:_next(nullptr)
			,_prev(nullptr)
			,_data(x)
		{}

		ListNode(T&& x)
			:_next(nullptr)
			, _prev(nullptr)
			, _data(move(x))
		{}

		template<class... Args>
		ListNode(Args&&... args)
			:_next(nullptr)
			, _prev(nullptr)
			, _data(std::forward<Args>(args)...)
		{}
	};

	//法一:写两个类(带const和不带const)
	//template<class T>
	//struct ListConstIterator
	//{
	//	typedef ListNode<T> Node;
	//	typedef ListConstIterator<T> Self;

	//	Node* _node;

	//	ListConstIterator(Node* node)
	//		:_node(node)
	//	{}

	//	// *it
	//	const T& operator*()
	//	{
	//		return _node->_data;
	//	}

	//	const T* operator->()
	//	{
	//		return &(_node->_data);
	//	}

	//	// ++it
	//	Self& operator ++()
	//	{
	//		_node = _node->_next;
	//		return *this;
	//	}

	//	// it++
	//	Self operator ++(int)
	//	{
	//		Self tmp(*this);//浅拷贝不需要写拷贝构造
	//		_node = _node->_next;
	//		return tmp;
	//	}

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

	//	Self operator --(int)
	//	{
	//		Self tmp(*this);//浅拷贝不需要写拷贝构造
	//		_node = _node->_prev;
	//		return tmp;
	//	}

	//	bool operator!=(const Self& it)
	//	{
	//		return _node != it._node;
	//	}

	//	bool operator ==(const Self& it)
	//	{
	//		return _node == it._node;
	//	}
	//};

	//法二:增加两个模板参数(编译器实例化生成了两个类)
	//正向迭代器
	template<class T,class Ref,class Ptr>
	struct ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T, Ref, Ptr> Self;

		Node* _node;

		ListIterator(Node* node)
			:_node(node)
		{}
		
		// *it
		//T& operator*()
		Ref operator*()
		{
			return _node->_data;
		}

		//T* operator->()
		Ptr operator->()
		{
			return &(_node->_data);
		}

		// ++it
		Self& operator ++()
		{
			_node = _node->_next;
			return *this;
		}

		// it++
		Self operator ++(int)
		{
			Self tmp(*this);//浅拷贝不需要写拷贝构造
			_node = _node->_next;
			return tmp;
		}

		Self& operator --()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator --(int)
		{
			Self tmp(*this);//浅拷贝不需要写拷贝构造
			_node = _node->_prev;
			return tmp;
		}

		bool operator!=(const Self& it)
		{
			return _node != it._node;
		}

		bool operator ==(const Self& it)
		{
			return _node == it._node;
		}
	};

	////反向迭代器(法一:改写ListIterator类;法二:写一个适用所有容器的迭代器适配器)
	//template<class T, class Ref, class Ptr>
	//struct RListIterator
	//{
	//	typedef ListNode<T> Node;
	//	typedef RListIterator<T, Ref, Ptr> Self;

	//	Node* _node;

	//	RListIterator(Node* node)
	//		:_node(node)
	//	{}

	//	// *it
	//	//T& operator*()
	//	Ref operator*()
	//	{
	//		return _node->_data;
	//	}

	//	//T* operator->()
	//	Ptr operator->()
	//	{
	//		return &(_node->_data);
	//	}

	//	// ++it
	//	Self& operator ++()
	//	{
	//		_node = _node->_prev;
	//		return *this;
	//	}

	//	// it++
	//	Self operator ++(int)
	//	{
	//		Self tmp(*this);//浅拷贝不需要写拷贝构造
	//		_node = _node->_prev;
	//		return tmp;
	//	}

	//	Self& operator --()
	//	{
	//		_node = _node->_next;
	//		return *this;
	//	}

	//	Self operator --(int)
	//	{
	//		Self tmp(*this);//浅拷贝不需要写拷贝构造
	//		_node = _node->_next;
	//		return tmp;
	//	}

	//	bool operator!=(const Self& it)
	//	{
	//		return _node != it._node;
	//	}

	//	bool operator ==(const Self& it)
	//	{
	//		return _node == it._node;
	//	}
	//};

	template<class T>
	class list
	{
		typedef ListNode<T> Node;
	public:
		//法一的typedef
		//typedef ListIterator<T> iterator;
		//typedef ListConstIterator<T> const_iterator;
		
		//法二的typedef
		typedef ListIterator<T,T&,T*> iterator;
		typedef ListIterator<T,const T&,const T*> const_iterator;

		////反向迭代器的法一
		//typedef RListIterator<T, T&, T*> reverse_iterator;
		//typedef RListIterator<T, const T&, const T*> const_reverse_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);
			//可以直接写return _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(_head->_prev);
		//}

		//reverse_iterator rend()
		//{
		//	return reverse_iterator(_head);
		//}

		//const_reverse_iterator rbegin()const
		//{
		//	return const_reverse_iterator(_head->_prev);
		//}

		//const_reverse_iterator rend()const
		//{
		//	return const_reverse_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());
		}

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;

			_size = 0;
		}

		list()
		{
			empty_init();
		}

		list(initializer_list<T> il)
		{
			empty_init();

			for (auto& e : il)
			{
				push_back(e);
			}
		}

		list(const list<T>& lt)
		{
			empty_init();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}

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

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

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

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

		/*void push_back(const T& x)
		{
			Node* newnode = new Node;
			Node* tail = _head->_prev;
			
			newnode->_data = x;

			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;
		}*/
		void push_back(const T& x)
		{
			insert(end(), x);
		}

		void push_back(T&& x)
		{
			insert(end(), move(x));
		}

		template<class... Args>
		void emplace_back(Args&&... args)
		{
			emplace(end(), std::forward<Args>(args)...);
		}


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

		void push_front(T&& x)
		{
			insert(begin(), move(x));
		}

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

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

		// 左值引用
		void insert(iterator pos,const T& val)
		{
			Node* cur = pos._node;
			Node* newnode = new Node(val);
			Node* prev = cur->_prev;

			//prev newnode cur
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			_size++;
		}

		// 右值引用
		void insert(iterator pos, T&& val)
		{
			//cout << "右值引用" << endl;
			Node* cur = pos._node;
			Node* newnode = new Node(move(val));
			Node* prev = cur->_prev;

			//prev newnode cur
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			_size++;
		}

		template<class... Args>
		void emplace(iterator pos, Args&&... args)
		{
			//cout << "emplace" << endl;
			Node* cur = pos._node;
			Node* newnode = new Node(std::forward<Args>(args)...);
			Node* prev = cur->_prev;

			//prev newnode cur
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			_size++;
		}

		iterator erase(iterator pos)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;
			delete cur;
			_size--;
			return iterator(next);
		}

		size_t size()const
		{
			return _size;
		}

		bool empty()const
		{
			return _size==0;
		}

	private:
		Node* _head;
		size_t _size=0;
	};
  • ReverseIterator.h
c 复制代码
#pragma once
//所有容器的反向迭代器
//迭代器适配器
namespace bit
{
	template<class Iterator,class Ref,class Ptr>
	struct ReverseIterator
	{
		Iterator _it;

		typedef ReverseIterator<Iterator, Ref, Ptr> Self;

		ReverseIterator(Iterator it)
			:_it(it)
		{}
		//T&
		Ref operator *()
		{
			Iterator tmp = _it;
			return *(--tmp);
		}
		//T*
		Ptr operator ->()
		{
			return &(operator *());
			//不能用_it->会报错
		}

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

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

		bool operator !=(const Self& s)
		{
			return _it != s._it;
		}
	};
}
  • Test.cpp
c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include"ReverseIterator.h"
#include"list.h"
#include<list>
#include<initializer_list>
#include<iostream>
#include<string>
#include <string.h>

using namespace std;

namespace bit
{
	// for test
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		const_iterator begin() const
		{
			return _str;
		}
		const_iterator end() const
		{
			return _str + _size;
		}
		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size)
		{
			cout << "string(char* str)-构造" << endl;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		void swap(string& s)
		{
			::swap(_str, s._str);
			::swap(_size, s._size);
			::swap(_capacity, s._capacity);
		}

		string(const string& s)
			:_str(nullptr)
		{
			cout << "string(const string& s) -- 拷贝构造" << endl;
			reserve(s._capacity);
			for (auto ch : s)
			{
				push_back(ch);
			}
		}

		// 移动构造
		string(string&& s)
		{
			cout << "string(string&& s) -- 移动构造" << endl;
			swap(s);
		}

		string& operator=(const string& s)
		{
			cout << "string& operator=(const string& s) -- 拷贝赋值" <<
				endl;
			if (this != &s)
			{
				_str[0] = '\0';
				_size = 0;
				reserve(s._capacity);
				for (auto ch : s)
				{
					push_back(ch);
				}
			}
			return *this;
		}
		// 移动赋值
		string& operator=(string&& s)
		{
			cout << "string& operator=(string&& s) -- 移动赋值" << endl;
			swap(s);
			return *this;
		}
		~string()
		{
			cout << "~string() -- 析构" << endl;
			delete[] _str;
			_str = nullptr;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				if (_str)
				{
					strcpy(tmp, _str);
					delete[] _str;
				}
				_str = tmp;
				_capacity = n;
			}
		}

		void push_back(char ch)
		{
			if (_size >= _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 4 : _capacity *
					2;
				reserve(newcapacity);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}
		const char* c_str() const
		{
			return _str;
		}

		size_t size() const
		{
			return _size;
		}
	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;
	};
}

void test1()
{
	//bit::test_list4();
	// 
	//bit::list<int> lt ;
	//lt.push_back(1);
	//lt.push_back(2);
	//lt.push_back(3);
	//lt.push_back(4);

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

void test2()
{
	bit::list<int> lt ;
	lt.push_front(1);
	lt.push_front(2);
	lt.push_front(3);
	lt.push_front(4);
	bit::list<int>::iterator it = lt.begin();

	while (it != lt.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

void test3()
{
	// 右值引用测试
	bit::list<string> lt;
	lt.push_front("a");
	lt.push_front("b");
	lt.push_front("c");
	lt.push_front("d");
	bit::list<string>::iterator it = lt.begin();

	while (it != lt.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

// 测试emplace
void test4()
{
	bit::list<bit::string> lt;
	// 传左值,跟push_back一样,走拷贝构造
	bit::string s1("111111111111");
	cout << "*********************************" << endl;
	lt.emplace_back(s1);
	cout << "*********************************" << endl;

	// 右值,跟push_back一样,走移动构造
	lt.emplace_back(move(s1));
	cout << "*********************************" << endl;

	// 直接把构造string参数包往下传,直接用string参数包构造string
	// 这里达到的效果是push_back做不到的
	lt.emplace_back("111111111111");
	cout << "*********************************" << endl << endl << endl;

	list<pair<bit::string, int>> lt1;
	// 跟push_back一样
	// 构造pair + 拷贝/移动构造pair到list的节点中data上
	pair<bit::string, int> kv("苹果", 1);
	lt1.emplace_back(kv);
	cout << "*********************************" << endl;

	// 跟push_back一样
	lt1.emplace_back(move(kv));
	cout << "*********************************" << endl;

	// 直接把构造pair参数包往下传,直接用pair参数包构造pair
	// 这里达到的效果是push_back做不到的
	//lt1.push_back({"苹果", 1 });
	lt1.emplace_back("苹果", 1);
	cout << "*********************************" << endl;
}

int main()
{
	//test2();
	test4();
	cout << endl;
	return 0;
}
相关推荐
一人の梅雨2 小时前
淘宝店铺全量商品接口深度开发:从分页优化到数据完整性保障
linux·windows·microsoft
王嘉俊9252 小时前
Qt 入门:构建跨平台 GUI 应用的强大框架
c语言·开发语言·c++·qt·入门·cpp
老歌老听老掉牙2 小时前
OpenCASCADE 点云拟合曲线与曲面:从零实现到工业级应用
c++·点云·opencascade
shenghaide_jiahu2 小时前
leetcode430:扁平化多级双向链表
数据结构·链表
失散133 小时前
软件设计师——03 数据结构(上)
数据结构·软考·软件设计师
Chance_to_win3 小时前
数据结构之双向链表
数据结构·链表
乌萨奇也要立志学C++3 小时前
【洛谷】二叉树专题全解析:概念、存储、遍历与经典真题实战
数据结构·c++·算法
CyHacker_10103 小时前
C++_day4
c++