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

template <typename T>
class ListItem


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

{
_value = value;
_next = next;
}

T value() const
{ return _value; }

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

ListItem* next() const
{ return _next; }

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

template <typename T>
class List


{
public:
~List()

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

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

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

{
if(_front == _end)

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

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

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

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

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

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


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

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


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

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

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

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

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

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

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

using namespace std;

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


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

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


{
List<int> mylist;

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

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

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

return;
}