书上已经讲得很清楚了,这里给出一个编译通过的例子。
 3mylist.h
 // file: 3mylist.h
// file: 3mylist.h
 #include <iostream>
#include <iostream>

 template <typename T>
template <typename T>
 class ListItem
class ListItem


 {
{
 public:
public:
 ListItem(T value, ListItem<T>* next)
    ListItem(T value, ListItem<T>* next)

 
     {
{
 _value = value;
        _value = value;
 _next = next;
        _next = next;
 }
    }

 T value() const
    T value() const  { return _value; }
{ return _value; }

 void value(T value)
    void value(T value)  { _value = value; }
{ _value = value; }

 ListItem* next() const
    ListItem* next() const  { return _next; }
{ return _next; }

 void next(ListItem* next)
    void next(ListItem* next)  { _next = next; }
{ _next = next; }
 //
    //
 private:
private:
 T _value;
    T _value;
 ListItem* _next;  // †ÎÏò´®ÁУ¨single linked list£©
    ListItem* _next;  // †ÎÏò´®ÁУ¨single linked list£©
 };
};

 template <typename T>
template <typename T>
 class List
class List


 {
{
 public:
public:
 ~List()
    ~List()

 
     {
{
 if(_front == _end) return;
        if(_front == _end) return;
 ListItem<T>* item = _front;
        ListItem<T>* item = _front;
 while(item != _end)
        while(item != _end)

 
         {
{
 ListItem<T>* iter = item;
            ListItem<T>* iter = item;
 item = item->next();
            item = item->next();
 delete iter;
            delete iter;
 }
        }
 
    
 }
    }
 void insert_front(T value)
    void insert_front(T value)

 
     {
{
 _front = new ListItem<T>(value, _front);
        _front = new ListItem<T>(value, _front);
 }
    }
 void insert_end(T value)
    void insert_end(T value)

 
     {
{
 if(_front == _end)
        if(_front == _end)

 
         {
{
 _front = new ListItem<T>(value, _front);
            _front = new ListItem<T>(value, _front);
 }
        }
 ListItem<T>* item = _front;
        ListItem<T>* item = _front;
 while(item->next() != _end)
        while(item->next() != _end)

 
         {
{
 item = item->next();
            item = item->next();
 }
        }
 item->next(new ListItem<T>(value, _end));
        item->next(new ListItem<T>(value, _end));
 }
    }
 void display(std::ostream &os = std::cout) const
    void display(std::ostream &os = std::cout) const

 
     {
{
 ListItem<T>* item = _front;
        ListItem<T>* item = _front;
 while(item != _end)
        while(item != _end)

 
         {
{
 os<<item->value()<<" ";
            os<<item->value()<<" ";
 item = item->next();
            item = item->next();
 }
        }
 os<<endl;
        os<<endl;
 }
    }

 ListItem<T>* front()
    ListItem<T>* front() { return _front;}
{ return _front;}

 ListItem<T>* end()
    ListItem<T>* end() { return _end;}
{ return _end;}
 //
    // 
 private:
private:
 ListItem<T>* _end;
    ListItem<T>* _end;
 ListItem<T>* _front;
    ListItem<T>* _front;
 long _size;
    long _size;
 };
};
3mylist-iter.h
 // file : 3mylist-iter.h
// file : 3mylist-iter.h
 #include "3mylist.h"
#include "3mylist.h"
 template <class Item> // Item可以是单向列表节点或双向列表节点。
template <class Item> // Item可以是单向列表节点或双向列表节点。
 struct ListIter       // 此处这个迭代器特定只为列表服务,因为其
struct ListIter       // 此处这个迭代器特定只为列表服务,因为其


 {                     // 独特的 operator++之故。
{                     // 独特的 operator++之故。   
 Item* ptr; // 保持与容器之间的一个联系
    Item* ptr; // 保持与容器之间的一个联系
 
    
 ListIter(Item* p = 0)  // default ctor
    ListIter(Item* p = 0)  // default ctor

 :  ptr(p)
        :  ptr(p)  { }
{ }
 
    
 // 不必实作 copy ctor,因为编译器提供的预设行为已足够。
    // 不必实作 copy ctor,因为编译器提供的预设行为已足够。
 // 不必实作 operator=,因为编译器提供的预设行为已足够。
    // 不必实作 operator=,因为编译器提供的预设行为已足够。


 Item& operator*()  const
    Item& operator*()  const  { return *ptr; }
{ return *ptr; }

 Item* operator->() const
    Item* operator->() const  { return  ptr; }
{ return  ptr; }
 
    
 // 以下两个operator++遵循标准作法,参见[Meyers96]条款6
    // 以下两个operator++遵循标准作法,参见[Meyers96]条款6      
 // (1) pre-increment operator
    // (1) pre-increment operator
 ListIter& operator++()
    ListIter& operator++()

 
     { ptr =  ptr->next(); return *this; }
{ ptr =  ptr->next(); return *this; }
 
    
 // (2) post-increment operator
    // (2) post-increment operator
 ListIter  operator++(int)
    ListIter  operator++(int)

 
     { ListIter tmp = *this; ++*this; return tmp; }
{ ListIter tmp = *this; ++*this; return tmp; }
 
    
 bool operator==(const ListIter& i) const
    bool operator==(const ListIter& i) const

 
     { return ptr == i.ptr; }
{ return ptr == i.ptr; }
 
    
 bool operator!=(const ListIter& i) const
    bool operator!=(const ListIter& i) const

 
     { return ptr != i.ptr; }
{ return ptr != i.ptr; }
 };
};
3mylist-iter.cpp
 // file : 3mylist-iter.cpp
// file : 3mylist-iter.cpp

 #include "stdafx.h"
#include "stdafx.h"
 #include "3mylist-iter.h"
#include "3mylist-iter.h"
 #include <iostream>
#include <iostream>

 using namespace std;
using namespace std;

 //  摘自 SGI <stl_algo.h>
//  摘自 SGI <stl_algo.h>
 template <class InputIterator, class T>
template <class InputIterator, class T>
 InputIterator find(InputIterator first,
InputIterator find(InputIterator first,
 InputIterator last,
                   InputIterator last,
 const T& value)
                   const T& value) 


 {
{
 while (first != last && (*first).value() != value)
    while (first != last && (*first).value() != value)
 ++first;
        ++first;
 return first;
    return first;
 }
}

 // 3mylist-iter-test.cpp
// 3mylist-iter-test.cpp
 void main()
void main()


 {
{
 List<int> mylist;
    List<int> mylist;

 for(int i=0; i<5; ++i)
    for(int i=0; i<5; ++i)  {
{
 mylist.insert_front(i);
        mylist.insert_front(i);
 mylist.insert_end(i+2);
        mylist.insert_end(i+2);
 }
    }
 mylist.display();     // 10 ( 4 3 2 1 0 2 3 4 5 6 )
    mylist.display();     // 10 ( 4 3 2 1 0 2 3 4 5 6 )
 ListIter<ListItem<int> > begin(mylist.front());
    ListIter<ListItem<int> > begin(mylist.front());
 ListIter<ListItem<int> > end(mylist.end());  // default 0, null
    ListIter<ListItem<int> > end(mylist.end());  // default 0, null
 ListIter<ListItem<int> > iter; // default 0, null
    ListIter<ListItem<int> > iter; // default 0, null

 // 执行结果:found. 3
    // 执行结果:found. 3
 iter = find(begin, end, 3);
    iter = find(begin, end, 3);
 if (iter == end)
    if (iter == end)
 cout << "not found" << endl;
        cout << "not found" << endl;
 else
    else
 cout << "found.  " << iter->value() << endl;
        cout << "found.  " << iter->value() << endl;

 // 执行结果:not found
    // 执行结果:not found
 iter = find(begin, end, 7);
    iter = find(begin, end, 7);
 if (iter == end)
    if (iter == end)
 cout << "not found" << endl;
        cout << "not found" << endl;
 else
    else
 cout << "found. " << iter->value() << endl;
        cout << "found. " << iter->value() << endl;

 return;
    return;
 }
}