C++ 迭代器问题
代码如下C++ 迭代器问题,c++,iterator,C++,Iterator,代码如下 template<class T> class arrayList { public: // constructor, copy constructor and destructor arrayList(int initialCapacity = 10); arrayList(const arrayList<T>&); ~arrayList() { delete[] element; }
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'
模板
类数组列表{
公众:
//构造函数、复制构造函数和析构函数
arrayList(int initialCapacity=10);
arrayList(常量arrayList&);
~arrayList(){
删除[]元素;
}
无缝类;
Seamlespointer begin(){
返回seamlessPointer(元素);
}
无间隙端(){
返回seamlessPointer(元素+列表大小);
}
//arrayList的迭代器
类迭代器
{
公众:
C/C++为双向迭代器所需的类型
typedef双向迭代器标记迭代器类别;
类型定义T值_类型;
typedef ptrdiff_t difference_type;
typedef T*指针;
typedef T&reference;
//建造师
迭代器(T*thePosition=0){position=thePosition;}
//解引用运算符
T&运算符*()常量{return*position;}
T*运算符->()常量{返回位置;}
//增量
迭代器和运算符++();
{++position;返回*this;}
迭代器运算符++(int);
//减量
迭代器和运算符——();
迭代器运算符--(int);
//平等测试
布尔运算符!=(常量迭代器右侧);
布尔运算符==(常量迭代器右侧);
受保护的:
T*位置;
};//迭代器类的结尾
类SeamlesPointer:PublicArrayList::iterator{
公众:
typedef随机访问迭代器标记迭代器类别;
类型定义T值_类型;
typedef ptrdiff_t difference_type;
typedef T*指针;
typedef T&reference;
//建造师
无缝道岔(T*位置);
seamlessPointer(const seamlessPointer&rhs);
//算术运算符
seamlessPointer运算符+(int n);
seamlessPointer运算符-(int n);
};
受保护的:
T*element;//用于保存列表元素的1D数组
int-arrayLength;//1D数组的容量
int listSize;//列表中的元素数
};
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_-algo.h:5250:4:错误:与'\uu last-\uu first'中的'operator-'不匹配
查看您的代码,通过将这些typedef包含在自己的迭代器实现中,您似乎获得了正确的想法。然而,当别的东西可以帮你的时候,为什么一开始就要麻烦呢?你看过标准吗?他们只是将typedef添加到您的代码中,这将帮助您开发新的迭代器类型。查看您的代码,通过将这些typedef包含在自己的迭代器实现中,您似乎获得了正确的想法。然而,当别的东西可以帮你的时候,为什么一开始就要麻烦呢?你看过标准吗?他们只是将typedef添加到您的代码中,这将帮助您开发新的迭代器类型。@Sean您说得对。从我的答案中删除了这一点,不过,现在我的答案不再是什么答案了。@Sean很高兴听到这个消息,但我的答案真的帮助你解决了问题吗?问题的最终原因是什么?@wheaties,事实上,我不知道确切的原因。我从原始代码中复制并粘贴了一次,它突然起作用了。可能是打字错误吧?无论如何,我应该感谢你的新想法。@Sean你说得对。从我的答案中删除了这一点,不过,现在我的答案不再是什么答案了。@Sean很高兴听到这个消息,但我的答案真的帮助你解决了问题吗?问题的最终原因是什么?@wheaties,事实上,我不知道确切的原因。我从原始代码中复制并粘贴了一次,它突然起作用了。可能是打字错误吧?不管怎样,我应该感谢你的新想法