代码如下
template<class T>
class arrayList {
public:
// constructor, copy constructor and destructor
arrayList(int initialCapacity = 10);
arrayList(const arrayList<T>&);
~arrayList() {
delete[] element;
}
class seamlessPointer;
seamlessPointer begin() {
return seamlessPointer(element);
}
seamlessPointer end() {
return seamlessPointer(element + listSize);
}
// iterator for arrayList
class iterator
{
public:
// typedefs required by C++ for a bidirectional iterator
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
// constructor
iterator(T* thePosition = 0) {position = thePosition;}
// dereferencing operators
T& operator*() const {return *position;}
T* operator->() const {return position;}
// increment
iterator& operator++();
{++position; return *this;}
iterator operator++(int);
// decrement
iterator& operator--();
iterator operator--(int) ;
// equality testing
bool operator!=(const iterator right) ;
bool operator==(const iterator right) ;
protected:
T* position;
}; // end of iterator class
class seamlessPointer: public arrayList<T>::iterator {
public:
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
// constructor
seamlessPointer(T *thePosition);
seamlessPointer(const seamlessPointer & rhs);
//arithmetic operators
seamlessPointer operator+(int n) ;
seamlessPointer operator-(int n) ;
};
protected:
T* element; // 1D array to hold list elements
int arrayLength; // capacity of the 1D array
int listSize; // number of elements in list
};
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:5250:4: error: no match for 'operator-' in '__last - __first'
答案 0 :(得分:1)
通过查看代码,您可以通过将这些typedef包含在您自己的迭代器实现中来获得正确的想法。但是,为什么在其他东西可以为你做的时候,首先要解决所有麻烦?您是否查看了iterator_traits或标准iterator?他们只是在你的代码中添加typedef,这将有助于你开发新的迭代器类型。