[cpp] // Iterator.cpp : 定義控制台應用程序的入口點。 // #include "stdafx.h" #include <Windows.h> #include <iostream> #include <string> #include <memory> using namespace std; // 對於C++,STL中已經包含迭代器的實現了 // 就不再單獨造車輪了 // 部分代碼見【STL源碼剖析】 // 來一個智能指針的示意 namespace TEST { template<class T> class auto_ptr { public: // 顯示構造函數 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; }; } // 智能指針的示例 // 申請的內存不需要釋放,沒有內存洩露 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; }; // 迭代器是一種智能指針 template<typename T> class List { public: List() { ListItem<T> *a = new ListItem<T>(0); // 沒有析構哦 _front = a; _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); } // 因為只保留了鏈表頭,所以插入操作很糾結,比較失敗 template<typename T> void TEST::List<T>::insert_end( T value ) { ListItem<T> *node = new ListItem<T>(value); ListItem<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 *this; } 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()); // 由於鏈表頭為空,所以最好由後往前迭代,第一個節點不能用 for (; iter != testList.end(); ) { ++iter; std::cout<<iter->value()<<endl; } www.2cto.com testList.display(); return 0; } //輸出結果: //Hello //5 //10 //20 //11 //10 //20 //11 //請按任意鍵繼續. . .