cpp中的list(上)

在string,vector和list中最为复杂的就是list,我想正是因为cpp中的list与c中的list区别导致我有些头昏欲裂吧,话不多说我吗开始解析list的基础框架

该图是他的结构模样

虚拟头节点:用于指向尾节点_head->_prev和指向头头节点_head->_next 

list基础框架

namespace xiaobo
{
    template<class T>
	struct __list_node//节点内的信息
	{
		__list_node<T>* _next;//指向下一节点
		__list_node<T>* _prev;//指向上一节点
		T _data;//数据

		//struct __list_node的构造函数
		__list_node(const T& x=T())
			:_data(x);
		    _next(nullptr),
			_prev(nullptr)
		{}
	};

    template<class T>
	class list//实现
	{
		typedef _list_node<T> Node;//设立节点内容的简称
	public:

		list()//初始化,设置虚拟头节点
		{
			_head = new Node();//虚拟头节点不需要数据存储
			_head->_next = _head;//前后指针都指向自己
			_head->_prev = _head;
		}

	private:
		Node* _head;
	};
}

push_back()

       void push_back(const T& x)//增加数据
		{
			Node* tail = _head->_prev;//找到实际尾节点
			Node* newNode = new Node(x);//创建新指针,且初始化

			//链接
			tail->_next = newNode;
			_head->_next = newNode;
			newNode->_prev = tail;
			newNode->_next = _head;
		}

iterator迭代器

(为什么不可以写typedef Node* iterator;来代替该struct,这是我所不知道和不了解的)

    template<class T>
	struct __list_iterator//迭代器
	{
		typedef __list_node<T> Node;
		Node* _node;//迭代器中的成员只有一个,和class list成员竟然一模一样
        //所以我并不理解为什么要专门分成两个类来写

		__list_iterator(Node* node);//拷贝函数
		    :_node(node)
		{}

	};

符号重载

T& operator*()//*it

        T& operator*()//返回Node指针数据
		{
			return _node->_data;
		}

 ++ it or it ++ 

__list_iterator<T>& operator++()//前置++,跳转到下一节点

__list_iterator<T>& operator++(int)//后置++

        //跳到下一个节点前置++
		__list_iterator<T>& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		//后置++
		__list_iterator<T>& operator++(int)
		{
			__list_iterator<T> tmp(*this);
			//生成一个tmp用于存储未++的值
			++(*this);//转换为下一节点
			return tmp;
		}

--it or it--

__list_iterator<T>& operator--()//前置--

__list_iterator<T>& operator--(int)//后置--

        __list_iterator<T>& operator--()//前置
		{
			_node = _node->_prev;
			return *this;
		}
		__list_iterator<T>& operator--(int)//后置
		{
			__list_iterator<t> tmp(*this);
			--(*this);
			return *this;
		}

bool operator!=(__list_iterator<T>& it)//判断是否为同一节点

        bool operator!=(__list_iterator<T>& it)//判断是否为同一节点
		{
			return _node != it->_node;
		}

T* operator->()

最难理解的部分,等等我举例就知道了

        T* operator->()//用于返回自定义类型
		{
			return &_node->_data;
		}

例如:

class Date//日期类型
{
public:
	Date(size_t year = 0, size_t month = 1, size_t day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	size_t _year;
	size_t _month;
	size_t _day;
};
int main()
{
	list<Date()> lt;
	//因为没有->的重载,无法使用->来表示自定义类型
	for (auto e : lt)
	{
		cout << e->_year << '-' 
			<< e->_month << '-' 
			<< e->_day << endl;
	}
	return 0;
}

一下就是今天的全部内容,感谢观看,也希望你能解答我的疑问,谢佬

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值