C++ STL源码分析之 list
来源:互联网 发布:练听力软件 编辑:程序博客网 时间:2024/06/01 15:01
STL中list与其他顺序容器实现不同因此不支持一部分容器操作,注:forward_list 有自己特有的insert 和emplace,vetor和string不支持push_front和emplace_front
STL的list是一个双向链表,而且实现的是个环状双向链表,因此一个指针即可代表整个链表。
为了符合STL对于前闭后开的区间要求,node指针指向特意置于尾端的一个空白节点,以符合这个要求。
here is the source code
//////////////////////////////////////////////////////////////////////////////// // list结点, 提供双向访问能力 //////////////////////////////////////////////////////////////////////////////// // -------- -------- -------- -------- // | next |---------->| next |---------->| next |---------->| next | // -------- -------- -------- -------- // | prev |<----------| prev |<----------| prev |<----------| prev | // -------- -------- -------- -------- // | data | | data | | data | | data | // -------- -------- -------- -------- //////////////////////////////////////////////////////////////////////////////// template <class T> struct __list_node { typedef void* void_pointer; void_pointer next; void_pointer prev; T data; }; // 至于为什么不使用默认参数, 这个是因为有一些编译器不能提供推导能力, // 而作者又不想维护两份代码, 故不使用默认参数 template<class T, class Ref, class Ptr> struct __list_iterator { typedef __list_iterator<T, T&, T*> iterator; // STL标准强制要求 typedef __list_iterator<T, Ref, Ptr> self; typedef bidirectional_iterator_tag iterator_category; typedef T value_type; typedef Ptr pointer; typedef Ref reference; typedef __list_node<T>* link_type; typedef size_t size_type; typedef ptrdiff_t difference_type; link_type node; //迭代器内部当然要有一个普通指针,指向list的节点 __list_iterator(link_type x) : node(x) {} __list_iterator() {} __list_iterator(const iterator& x) : node(x.node) {} // 在STL算法中需要迭代器提供支持 bool operator==(const self& x) const { return node == x.node; } bool operator!=(const self& x) const { return node != x.node; } // 以下对迭代器取值(dereference),取的是节点的数据值 reference operator*() const { return (*node).data; } // 以下是迭代器的成员存取运算子的标准做法 pointer operator->() const { return &(operator*()); } // 前缀自加,对迭代器累加1,就是前进一个节点 self& operator++() { node = (link_type)((*node).next); return *this; } // 后缀自加, 需要先产生自身的一个副本, 然会再对自身操作, 最后返回副本 self operator++(int) { self tmp = *this; ++*this; return tmp; } // 前缀自减 self& operator--() { node = (link_type)((*node).prev); return *this; } self operator--(int) { self tmp = *this; --*this; return tmp; } }; //////////////////////////////////////////////////////////////////////////////// // list不仅是个双向链表, 而且还是一个环状双向链表 //////////////////////////////////////////////////////////////////////////////// // end() 头结点 begin() // ↓ ↓ ↓ // -------- -------- -------- -------- // ---->| next |---------->| next |---------->| next |---------->| next |------ // | -------- -------- -------- -------- | // | --| prev |<----------| prev |<----------| prev |<----------| prev |<--| | // | | -------- -------- -------- -------- | | // | | | data | | data | | data | | data | | | // | | -------- -------- -------- -------- | | // | | | | // | | -------- -------- -------- -------- | | // ---|-| next |<----------| next |<----------| next |<----------| next |<--|-- // | -------- -------- -------- -------- | // ->| prev |---------->| prev |---------->| prev |---------->| prev |---- // -------- -------- -------- -------- // | data | | data | | data | | data | // -------- -------- -------- -------- ////////////////////////////////////////////////////////////////////////////////
deque
0 0
- C++ STL源码分析之 list
- STL源码分析--list
- <stl>list::sort源码分析
- stl源码剖析之vector,list,deque迭代器分析
- STL之list源码剖析
- STL 之 list 源码剖析
- STL源码剖析之list
- STL之list源码剖析
- c++STL之顺序容器(vector、deque、list)分析
- C++STL之list容器
- STL源码分析--list中的sort算法
- [C++][STL] C++ STL 之 List
- STL源码分析之感想
- STL源码分析之Vector
- STL源码分析之Vector
- STL之pair源码分析
- STL源码分析之大顶堆
- STL源码分析之hashtable
- Android开发实践:Java层与Jni层的数组传递
- Oracle execute immediate 拼接条件 参数化
- POJ2777 Count Color 线段树
- 算法导论 思考题 10-2
- 毕设第一步
- C++ STL源码分析之 list
- js自适应
- 备份mysql数据库注意
- DedeCMS远程发布使用教程
- 【Codeforces 576D】 Flights for Regular Customers
- Error executing DDL via JDBC Statement
- j2ee服务端集成支付宝扫码支付功能
- dubbo 教程
- Linux中profile、bashrc、bash_profile之间的区别和联系