最近在复习STL,感觉再看的时候比刚开始学的时候通透很多。以前模拟实现了一个STL库,最近复习完又重构了一遍。代码放出来以供后面学习。如果有写的不好的地方欢迎大家批评指正。
STL_List.h
1 #pragma once 2 #include"STL_Iterator.h" 3 4 template<class T> 5 struct _List_Node 6 { 7 _List_Node* _prev; 8 _List_Node* _next; 9 T _data; 10 _List_Node() 11 { 12 13 } 14 _List_Node(const T& x) 15 :_data(x) 16 , _prev(NULL) 17 , _next(NULL) 18 { 19 20 } 21 }; 22 23 template<class T, class Ref, class Ptr> 24 struct _List_Iterator 25 { 26 typedef BidirectionalIterator_tag IteratorCategory; 27 typedef _List_Iterator<T, T&, T*> Iterator; 28 typedef T ValueType; 29 typedef T& Reference; 30 typedef T* Pointer; 31 typedef _List_Node<T> *LinkType; 32 typedef _List_Iterator<T, Ref, Ptr> Self; 33 typedef int DifferenceType; 34 LinkType _node; 35 36 _List_Iterator() 37 { 38 39 } 40 _List_Iterator(LinkType x) 41 :_node(x) 42 { 43 } 44 Reference operator*() 45 { 46 return _node->_data; 47 } 48 Pointer operator->() 49 { 50 return &(_node->_data); 51 } 52 Iterator operator++() 53 { 54 _node = _node->_next; 55 return *this; 56 } 57 Iterator operator++(int) 58 { 59 Iterator tmp; 60 tmp = *this; 61 _node = _node->_next; 62 return tmp; 63 } 64 65 Iterator operator--() 66 { 67 _node = _node->_prev; 68 return *this; 69 } 70 Iterator operator--(int) 71 { 72 Iterator tmp = *this; 73 _node = _node->_prev; 74 return tmp; 75 } 76 77 bool operator!=(const Self& x) 78 { 79 return _node != x._node; 80 } 81 82 bool operator==(const Self& x) 83 { 84 return _node == x._node; 85 } 86 87 88 }; 89 90 template<class T, class Alloc = Alloc> 91 class List 92 { 93 public: 94 typedef _List_Node<T>* LinkType; 95 typedef _List_Iterator<T, T&, T*> Iterator; 96 typedef SimpleAlloc<_List_Node<T>, Alloc> ListNodeAllocator; 97 98 List() 99 { 100 _node = new _List_Node<T>(); 101 _node->_next = _node; 102 _node->_prev = _node; 103 } 104 ~List() 105 { 106 Destory(Begin(), End()); 107 Iterator it = Begin(); 108 while (it != End()) 109 { 110 ListNodeAllocator::Deallocate(it._node); 111 } 112 } 113 114 115 Iterator Begin() 116 { 117 return _node->_next; 118 } 119 120 Iterator End() 121 { 122 return _node; 123 } 124 125 126 127 128 Iterator Insert(Iterator pos, const T& x) 129 { 130 LinkType prev = pos._node->_prev; 131 LinkType tmp = ListNodeAllocator::Allocate(); 132 Construct(tmp, x); 133 //LinkType tmp = new _List_Node<T>(x); 134 prev->_next = tmp; 135 pos._node->_prev = tmp; 136 tmp->_next = pos._node; 137 tmp->_prev = prev; 138 return tmp; 139 } 140 141 142 void PushBack(const T& x) 143 { 144 Insert(End(), x); 145 } 146 147 148 void PushFront(const T& x) 149 { 150 Insert(Begin, x); 151 } 152 153 154 //reverse 155 //remove 156 //empty 157 //size 158 //unique 159 //merge 160 protected: 161 LinkType _node; 162 }; 163 164 165 void TestList() 166 { 167 List<int> l; 168 l.PushBack(1); 169 l.PushBack(2); 170 l.PushBack(3); 171 l.PushBack(4); 172 l.PushBack(5); 173 174 List<int>::Iterator it; 175 List<int>::Iterator begin = l.Begin(); 176 List<int>::Iterator end = l.End(); 177 for (it = l.Begin(); it != l.End(); it++) 178 { 179 cout << *it << " "; 180 } 181 cout << "Distance" << Distance(begin, end) << endl; 182 }