List library functions in the compilation still have a lot of problems, in the compilation of the source code, some of the content has not been understood, in the later study will be more in-depth study, I hope you can make suggestions and criticisms of my problems, thank you ~
The specific code is as follows:
#include <iostream>using namespace std;//The basic functions of a doubly linked list implemented with iterators and spatial configurator template<class _ty,class _a = allocator< _ty> >//define Template Classes Class List//list class {public: typedef size_t SIZE_TYPE; Type redefinition protected:struct _node; Structural body _nodefriend struct _node; friend typedef _node* _NODEPTR; Type redefined struct _node//struct definition {_nodeptr _next,_prev;_ty _value;}; Protected:_a allocator; _nodeptr _head; Size_type _size;public:typedef list<_ty,_a> _myt;typedef _a allocator_type; The type is a synonym for template parameter a typedef size_t SIZE_TYPE; Positive integer type typedef ptrdiff_t DIFFERENCE_TYPE; Integer type typedef _ty* pointer; Pointer to the _ty typedef const _TY* Const_pointer; Constant pointer to _ty typedef _ty& Reference; References to _tytypedef const _ty& Const_reference;//_ty constants refer to typedef _ty VALUE_TYPE; Object Type _tyclass iterator; The iterator class const_iterator that accesses the list; Constant iterator to access list friend class Const_iterator; Friend class Const_iterator:public _bidit<_ty, difference_type>//inheritance {public: Total members _nodeptr _mynode () const//points to the current node {return (_PTR);} Const_iterator ()//default constructor {}const_iterator (_nodeptr _p): _ptr (_p)//parameter is pointer to constructor {}con St_iterator (const iterator& _x): _ptr (_X._PTR)//parameter is the constructor of the const iterator accessing the list {}const_reference operator* () const/ /Gets the value of the constant of the current _ty {return _ptr->_value;} Const_pointer operator-> () const//? {return (&**this);} const_iterator& operator++ ()///front + + Reference {_ptr = _ptr->_next;return (*this);} Const_iterator operator++ (int)//post + + + Reference {const_iterator _tmp = *this;++*this;return (_tmp);} Const_iterator& operator--()//front--reference {_ptr = _ptr->_prev;return (*this);} Const_iterator operator--(int)//post-reference {const_iterator _tmp = *this;--*this;return (_tmp);} BOOL operator== (const const_iterator& _x) const//Determine equality {return (_ptr = = _x._ptr);} BOOL Operator!= (const const_iterator& _x) const//judgment is not equal to {return (!) ( *this = = _x)); }protected://protection member _nodeptr _ptr;}; Friend class iterator; Friend class Iterator:public Const_iterator//Inheritance {Public:iterator ()//default construction function {}iterator (_nodeptr _p): Const_iterator (_p)//parameter is pointer to constructor {}reference operator* () const//Get current _ty The value {return _ptr->_value;} _ty* operator-> () const//? {return (&**this);} iterator& operator++ ()///front + + Reference {_ptr = _ptr->_next;return (*this);} Iterator operator++ (int)//post + + + Reference {iterator _tmp = *this;++*this;return (_tmp);} iterator& operator--()//front--reference {_ptr = _ptr->_prev;return (*this);} Iterator operator--(int)//post-reference {iterator _tmp = *this;--*this;return (_tmp);} BOOL operator== (const iterator& _x) const//Determine equality {return (_ptr = = _x._ptr);} BOOL Operator!= (const iterator& _x) const//judgment is not equal to {return (!) ( *this = = _x));}; Public:_nodeptr _buynode (_nodeptr _narg = 0, _nodeptr _parg = 0)//purchase Node {_nodeptr _s = (_nodeptr) allocator._charalloc ( Open Space 1 * sizeof (_node)); _s->_next = _narg! = 0? _narg: _s; _s->_prev = _parg! = 0? _parg: _s;return (_s); }void _freenode (_nodeptr _s)//Free space {allocator.deallocate (_s, 1);} void _splice (iterator _p, _myt& _x, iterator _f, iterator _l)//stitching {if (allocator = = _x.allocator)//Will two Stitching together, _x empty {(_l._mynode ())->_prev->_next = _p._mynode ();(_f._mynode ())->_prev->_next = _l._mynode ();(_p._mynode ())->_prev->_next = _f._mynode (); _nodeptr _S = (_P._Mynode ())- >_Prev; (_p._mynode ())->_prev = (_l._mynode ())->_prev; (_l._mynode ())->_prev = (_f._mynode ())->_prev ;(_f._mynode ())->_prev = _s; }else//Add the list value in _x to _p, _x empty {insert (_p, _f, _l); _x.erase (_f, _l);}} void _xran () const//throws Exception {_throw (Out_of_range, "Invalid list<t> subscript"); }public:size_type size () const//Length {return (_size);} BOOL Empty () const//Empty {return (size () = = 0);} _a get_allocator () const//Return Space Configurator {return (allocator);} void Resize (Size_type _n, _ty _x = _ty ())//Redefine list length {if (size () < _n) {Insert (end (), _n-size (), _x);} Else{while (_n < Size ()) {Pop_back ();}}} Size_type max_size () const//Return list maximum possible length {return (allocator. Max_size ());} Reference Front ()//returns the reference of the first element {return (*begin ());} Const_reference Front () const//Returns a constant reference to the first element {return (*begin ());} Reference back ()//returns the reference of the last element {return (* (--end ()));} Const_reference back () const//Returns a constant reference to the last element {return (* (--end ()));} _myt& operator= (const _myt& _x)//operator Constructor {if (this! = &_x) {iterator _f1 = begin (); it Erator _L1 = end (); Const_iterator _f2 = _x.begin (); Const_iterator _l2 = _x.end (); for (; _f1! = _l1 && _f2! = _L2; ++_F1, ++_f2) {*_f1 = *_f2;} Erase (_F1, _L1); Insert (_L1, _F2, _L2);} return (*this);} Public:explicit list (): _head (_buynode ()), _size (0)//empty list {}explicit list (const _ty& _v): _head (_buynode ()) , _size (0)//Build a list of elements with _v default value is 0 {insert (begin (), _v,0);} Explicit list (Size_type _n, const _ty& _v): _head (_buynode ()), _size (0)//Build a list with _v elements, values are _n{insert (Begin (), _n, _v);} List (const _myt& _x): _head (_buynode ()), _size (0)//build a _x copy linked list {insert (Begin (), _x.begin (), _x.end ());} List (const _ty *_f, const _ty *_l): _head (_buynode ()), _size (0)//element of an area [_first, _last]. {Insert (Begin (), _f, _l);} typedef const_iterator _it;list (_it _f, _it _l): _head (_buynode ()), _size (0)//Const iterator {insert (Begin (), _f, _l);} Iterator begin ()//Iterator first node {return iterator (_head->_next);} Const_iterator begin () const//constant iterator first node {return (Const_iterator (_head->_next));} Iterator end ()//returns the pointer to the next position of the last element {return iterator (_head);} Const_iterator End () const//returns the pointer {return (const_iterator (_head)) to the next position of the last element; Iterator Insert (iterator _p,const _ty& _x)//inserting node {_p _nodeptr = _s before _p._mynode (); _s->_prev = _bu Ynode (_s,_s->_prev); _s = _s->_prev;_s->_prev->_next = _s;_s->_value = _x;++_size;return (iterator (_S)) ;} void Insert (iterator _p,size_type _m, const _ty& _x)//_p _m node {for (; 0 < _x) {Insert (_m;--_m)} before _p,_x;}} void Insert (iterator _p, const _ty *_f, const _ty *_l)//insert _p to _f node before _l {for (; _f! = _l; ++_f) {insert (_p, *_f);}} typedef const_iterator _it;void Insert (iterator _p, _it _f, _it _l)//before _p Insert iterator _f to _l node {for (; _f! = _l; ++_f) {Insert (_p, *_f);}} void Push_front (const _ty& _x)//Add an element to the chain header {Insert (Begin (), _x);} void Pop_front ()//delete one element of the linked header {erase (begin ());} void push_back (const _ty& _x)//Add an element to the end of the list {insert (end (), _x);} void Pop_back ()//delete one element of the chain footer {erase (--end ());} void Assign (Size_type _n, const _ty& _x)//Assigned value {Erase (Begin (), End ()), insert (Begin (), _n, _x);} Iterator Erase (iterator _p)//delete _p element {_nodeptr _s = (_p++). _mynode (); _s->_prev->_next = _s->_next; _s->_next->_prev = _s->_prev;_freenode (_s);--_size;return (_p);} Iterator Erase (iterator _f, iterator _l)//delete the _f to _l area of the element {while (_f! = _l) {erase (_f++);} return (_f);} void Clear () Delete all elements {erase (begin (), end ());} void Show ()//stl the non-existent function in the source code, adding it yourself, in order to make the test more convenient {_nodeptr _p = _head->_next;while (_p! = _head ) {cout<<_p->_value<< "--"; _p = _p->_next;} cout<< "Over" <<ENDL;} ~list ()//destructor {erase (Begin (), End ()), _freenode (_head), _head = 0, _size = 0;} Public:void swap (_myt& _x)//Exchange two list {if (allocator = = _x.allocator)//If the Space Configurator is the same, then simply swap the head node and size {Std::swap (_head, _x._ Head); Std::swap (_size, _x._size);} else//Otherwise the list is spliced into each other's space {iterator _p = begin () splice (_p, _x); _x.splice (_x.begin (), *this, _p, End ());}} friend void swap (_myt& _x, _myt& _y)//Exchange two linked list {_x.swap (_y);} void Splice (iterator _p, _myt& _x)//concatenation of two linked lists {if (!_x.empty ()) {_splice (_p, _x, _x.begin (), _x.end ()); _size + = _x._ size;_x._size = 0;}} void Splice (iterator _p, _myt& _x, iterator _f)//stitching on two linked lists {iterator _l = _f;if (_p! = _f && _p! = ++_l) {_splic E (_p, _x, _f, _l); ++_size;--_x._size; }}void Splice (iterator _p, _myt& _x, Iterator _f, iterator _l)//stitching on two linked lists {if (_f! = _l) {if (&_x! = this) {Difference_type _n = 0;_distance (_f, _l, _n); _siz E + = _n;_x._size-= _n; }_splice (_p, _x, _f, _l); }}void Remove (const _ty& _V)//Remove all elements of _v from the linked list {iterator _l = end (); for (iterator _f = begin (); _f! = _l;) {if (*_f = = _v) {erase (_f++);} Else{++_f;}}} void unique ()//delete all duplicate elements to create a linked list with a unique element value,//The list does not have duplicate elements {iterator _f = begin (), _l = End (), and if (_f! = _l) {for (iterator _m = _f; ++_m! = _l; _m = _f) {if (*_f = = *_m) {erase (_m);} Else{_f = _m;}}} void merge (_myt& _x)//Merges the current list *this and X, merges x to NULL,//inserts elements from X into the current list, does not allow two linked lists the same {if (&_x! = this) {iterator _f1 = begin () , _L1 = end (), iterator _f2 = _x.begin (), _l2 = _x.end (); while (_f1! = _l1 && _f2! = _l2) {if (*_f2 < *_f1) {Itera Tor _mid2 = _f2;_splice (_f1, _x, _f2, ++_mid2); _f2 = _mid2; }else{++_f1;}} if (_f2! = _l2) {_splice (_L1, _x, _f2, _L2);} _size + = _x._size;_x._size = 0; }}void reverse ()//reverses the order of elements in the list {if (2 <= size ()) {Iterator _l =End (); for (iterator _f = ++begin (); _f! = _l;) {Iterator _m = _f;_splice (Begin (), *this, _m, ++_f);}}} void sort ()//Sort the list by default criteria? {if (2 <= size ()) {const size_t _MAXN = 15;_myt _x, _a[_maxn + 1];size_t _n = 0;while (!empty ()) {_x.splice (_x.begin ()), * This, begin ()); size_t _i;for (_i = 0; _i < _n &&!_a[_i].empty (); ++_i) {_a[_i].merge (_x); _a[_i].swap (_x);} if (_i = = _MAXN) {_a[_i].merge (_x);} Else{_a[_i].swap (_x); if (_i = = _n) {++_n;}}} while (0 < _n) {merge (_a[--_n]);}}}; Template<class _ty, class _a> the inline//template class determines whether equal bool operator== (const list<_ty, _a>& _x, Const List<_ty, _a>& _y) {return (_x.size () = = _y.size () && equal (_x.begin (), _x.end (), _y.begi N ()));} Template<class _ty, class _a> inline//template class to determine whether to unequal bool operator!= (const list<_ty, _a>& _x,con St List<_ty, _a>& _y) {return (!) ( _x = = _y));} Template<class _ty, class _a> inline//template class Compare the size of the list bool OPERATOR≪ (const list<_ty, _a>& _x, const list<_ty, _a>& _y) {return (Lexicographical_compare (_x.begin (), _X . End (), _y.begin (), _y.end ())); }template<class _ty, class _a> inline//template class Compare the size of the list _y < _xbool operator> (const list<_ty, _a> ;& _x, const list<_ty, _a>& _y) {return (_y < _x);} Template<class _ty, class _a> inline//template class Compare the size of the list! (_y < _x) bool operator<= (const list<_ty, _a>& _x,const list<_ty, _a>& _y) {return (!) ( _y < _x)); }template<class _ty, class _a> inline//template class Compare the size of the list! (_x < _y) bool operator>= (const list<_ty, _a>& _x,const list<_ty, _a>& _y) {return (!) ( _x < _y)); }void Main () {list<int> C0; Empty list list<int> C1 (3); Build a list of elements with three default values that are 0 list<int> C2 (C1); Build a C1 copy linked list list<int> C3 (C1.begin (), C1.end ()); Elements containing C1 an area [_first, _last). int ar[5] = {12,23,34,45,56};list<int> itlist (ar,ar+5); List<int> MyList (3,2) with array assignment; Build a list of three elements with values of 2mylist.show ();list<int> youlist (3,3); Youlist.show (); Swap (mylist, youlist); Exchange of two linked lists Mylist.show (); Youlist.show (); Mylist.swap (youlist); Mylist.show (); Youlist.show (); list<int>:: Iterator I1 = Itlist.begin (); Itlist.splice (i1,mylist); Two linked lists are combined with itlist.show (); Itlist.remove (23); Itlist.show (); Itlist.unique (); Itlist.show (); Itlist.merge (mylist); Itlist.show (); Mylist.show (); Itlist.reverse (); Itlist.show (); Itlist.sort (); Itlist.show ();cout<<operator< (itlist,mylist) <<endl;cout<< Operator> (Itlist,mylist) <<endl;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Implementation of the "C++/stl" list (with Space Configurator and iterators)