6.C++ STL之list

本文深入探讨C++ STL中的list容器,分析了sizeof(list)的变化及其原因,强调了迭代器在list中的作用,特别是operator*()和operator++()的功能。同时,解释了list内部节点结构的设计,包括node的parent属性和精确的type。文章还介绍了list的继承结构,包括_list_base和_list_impl,以及allocator的角色。
摘要由CSDN通过智能技术生成

源代码

G2.9 sizeof(list)=4 link_node * 一个指针在32位系统是4字节
G4.9 sizeof(list)=8

template<class T,class Alloc=alloc>
class list{
   
	protected:
		typedef _list_node<T> list_node;
	public:
		typedef list_node* link_type;
		typedef _list_iterator<T,T&,T*> iterator;//模拟指针
	protected:
		link_type node;
		......
};

template <class T>
struct _list_node{
   
	typedef void* void_pointer;
	void_pointer prev;//指向void 这种指针,需要正确的转型
	void_pointer next;
	T data;
};

template<class T,class Ptr>
struct _list_iterator{
   
	typedef T value_type;
	typedef Ptr pointer;
	typedef Ref reference;
};
//需要自动进入查看next和pre指针,当++就智能的查看next

在这里插入图片描述
灰色的结点是为了实现前闭后开区间

迭代器

在这里插入图片描述

//G2.9
template<class T,class Ptr
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值