[Cpp] // Iterator. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <Windows. h> # include <iostream> # include <string> # include <memory> using namespace std; // for C ++, STL already contains the implementation of the iterator. // you will not create a wheel independently. // for some code, see [STL source code analysis] // use a smart pointer to indicate namespace TEST {template <class T> class auto_ptr {public: // display the constructor explicit auto_ptr (T * p = NULL): pointee (p) {} template <class U> auto_ptr (auto_ptr <U> & rhs): pointee (rhs. release ()){}~ Auto_ptr () {delete pointee;} template <class U> auto_ptr <T> & operator = (auto_ptr <U> & rhs) {if (this! = & Rhs) {reset (rhs. release (); return * this ;}t & operator * () const {return * pointee;} T * operator-> () const {return pointee ;} T * get () const {return pointee;} protected: T * release () {T * p = new T; memcpy (p, pointee, sizeof (T )); delete this; return p;} void reset (T * p) {if (pointee) {delete pointee; pointee = NULL;} pointee = p;} private: T * pointee ;};}// example of a smart pointer // The applied memory does not need to be released, no Memory leakage void func () {TEST: auto_ptr <string> ps (new string ("Hello"); cout <* ps <endl; cout <ps-> size () <endl;} namespace TEST {template <typename T> class ListItem {public: ListItem (T vl): _ value (vl ), _ next (NULL) {} T value () const {return _ value;} ListItem * next () const {return _ next;} void SetNext (ListItem <T> * p) {_ next = p;} private: T _ value; ListItem * _ next;}; // The iterator is a smart pointer template <ty Pename T> class List {public: List () {ListItem <T> * a = new ListItem <T> (0); // No destructor Oh _ front =; _ front-> SetNext (NULL); _ size = 0;} void insert_end (T value); void display (std: ostream & OS = std: cout) const; listItem <T> * front () {return _ front;} ListItem <T> * end () {ListItem <T> * end = _ front; while (1) {if (end-> next () {end = end-> next () ;}else {break ;}} return end ;}private: ListItem <T> * _ Front; long _ size ;}; template <typename T> void TEST: List <T >:: display (std: ostream & OS/* = std :: cout */) const {ListItem <T> * end = NULL; end = _ front-> next (); ListItem <T> * before = _ front; do {OS <end-> value () <endl; end = end-> next () ;}while (end) ;}// because only the linked list header is retained, therefore, the insert operation is quite tangled, and the comparison failed template <typename T> void TEST: List <T >:: insert_end (T value) {ListItem <T> * node = new ListItem <T> (value); Lis TItem <T> * end = this-> end (); // end = _ front; // while (1) // {// if (end-> next () // {// end = end-> next (); ///} // else // {// break; //} end-> SetNext (node); _ size ++ ;} template <typename Item> struct ListIter {Item * ptr; ListIter (Item * p = NULL): ptr (p) {} Item & operator * () const {return * ptr ;} item * operator-> () const {return ptr;} ListIter & operator ++ () {ptr = ptr-> next (); return * thi S;} bool operator = (const ListIter & I) {return ptr = I. ptr;} bool operator! = (Const ListIter & I) {return ptr! = I. ptr ;}}}int _ tmain (int argc, _ TCHAR * argv []) {func (); TEST: List <int> testList; testList. insert_end (10); testList. insert_end (20); testList. insert_end (11); TEST: ListIter <TEST: ListItem <int> iter (testList. front (); // because the linked list header is empty, it is best to iterate from the back to the front. The first node cannot use for (; iter! = TestList. end ();) {++ iter; std: cout <iter-> value () <endl;} www.2cto.com testList. display (); return 0;} // output result: // Hello // 5 // 10 // 20 // 11 // 10 // 20 // 11 // 20 // 11 // 11 // press any key to continue...