我从来没有实现类似STL的迭代器,我试图理解如何基于指针实现一个非常基本的东西.一旦我有了这门课程,我就可以修改它来做更复杂的事情.因此,这是第一步,我需要它坚如磐石才能理解如何编写自己的迭代器(没有boost).
我写了下面的代码,我知道它有错误.你能帮助我正确设计一个受其启发的Random Access Iterator类:
template<Type> class Container<Type>::Iterator : public std::iterator<random_access_iterator_tag, Type>
{
// Lifecycle:
public:
Iterator() : _ptr(nullptr) {;}
Iterator(Type* rhs) : _ptr(rhs) {;}
Iterator(const Iterator &rhs) : _ptr(rhs._ptr) {;}
// Operators : misc
public:
inline Iterator& operator=(Type* rhs) {_ptr = rhs; return *this;}
inline Iterator& operator=(const Iterator &rhs) {_ptr = rhs._ptr; return *this;}
inline Iterator& operator+=(const int& rhs) {_ptr += rhs; return *this;}
inline Iterator& operator-=(const int& rhs) {_ptr -= rhs; return *this;}
inline Type& operator*() {return *_ptr;}
inline Type* operator->() {return _ptr;}
inline Type& operator[](const int& rhs) {return _ptr[rhs];}
// Operators : arithmetic
public:
inline Iterator& operator++() {++_ptr; return *this;}
inline Iterator& operator--() {--_ptr; return *this;}
inline Iterator& operator++(int) {Iterator tmp(*this); ++_ptr; return tmp;}
inline Iterator& operator--(int) {Iterator tmp(*this); --_ptr; return tmp;}
inline Iterator operator+(const Iterator& rhs) {return Iterator(_ptr+rhs.ptr);}
inline Iterator operator-(const Iterator& rhs) {return Iterator(_ptr-rhs.ptr);}
inline Iterator operator+(const int& rhs) {return Iterator(_ptr+rhs);}
inline Iterator operator-(const int& rhs) {return Iterator(_ptr-rhs);}
friend inline Iterator operator+(const int& lhs, const Iterator& rhs) {return Iterator(lhs+_ptr);}
friend inline Iterator operator-(const int& lhs, const Iterator& rhs) {return Iterator(lhs-_ptr);}
// Operators : comparison
public:
inline bool operator==(const Iterator& rhs) {return _ptr == rhs._ptr;}
inline bool operator!=(const Iterator& rhs) {return _ptr != rhs._ptr;}
inline bool operator>(const Iterator& rhs) {return _ptr > rhs._ptr;}
inline bool operator<(const Iterator& rhs) {return _ptr < rhs._ptr;}
inline bool operator>=(const Iterator& rhs) {return _ptr >= rhs._ptr;}
inline bool operator<=(const Iterator& rhs) {return _ptr <= rhs._ptr;}
// Data members
protected:
Type* _ptr;
};
Run Code Online (Sandbox Code Playgroud)
非常感谢你.
cub*_*l42 10
您的代码存在以下问题:
Iterator(Type* rhs) 可以是私人的,Container可以被标记为Iterator朋友,但这不是绝对必要的.operator=(Type* rhs)是个坏主意.这不是什么类型的安全.Iterator,而不是Iterator &.std::iterator<std::random_access_iterator_tag, Type>::difference_type而不是const int &.const.有用的资源:RandomAccessIterator @ cppreference.com
以下是您的代码的固定版本:
template<typename Type>
class Container<Type>::Iterator : public std::iterator<std::random_access_iterator_tag, Type>
{
public:
using difference_type = typename std::iterator<std::random_access_iterator_tag, Type>::difference_type;
Iterator() : _ptr(nullptr) {}
Iterator(Type* rhs) : _ptr(rhs) {}
Iterator(const Iterator &rhs) : _ptr(rhs._ptr) {}
/* inline Iterator& operator=(Type* rhs) {_ptr = rhs; return *this;} */
/* inline Iterator& operator=(const Iterator &rhs) {_ptr = rhs._ptr; return *this;} */
inline Iterator& operator+=(difference_type rhs) {_ptr += rhs; return *this;}
inline Iterator& operator-=(difference_type rhs) {_ptr -= rhs; return *this;}
inline Type& operator*() const {return *_ptr;}
inline Type* operator->() const {return _ptr;}
inline Type& operator[](difference_type rhs) const {return _ptr[rhs];}
inline Iterator& operator++() {++_ptr; return *this;}
inline Iterator& operator--() {--_ptr; return *this;}
inline Iterator operator++(int) const {Iterator tmp(*this); ++_ptr; return tmp;}
inline Iterator operator--(int) const {Iterator tmp(*this); --_ptr; return tmp;}
/* inline Iterator operator+(const Iterator& rhs) {return Iterator(_ptr+rhs.ptr);} */
inline difference_type operator-(const Iterator& rhs) const {return Iterator(_ptr-rhs.ptr);}
inline Iterator operator+(difference_type rhs) const {return Iterator(_ptr+rhs);}
inline Iterator operator-(difference_type rhs) const {return Iterator(_ptr-rhs);}
friend inline Iterator operator+(difference_type lhs, const Iterator& rhs) {return Iterator(lhs+rhs._ptr);}
friend inline Iterator operator-(difference_type lhs, const Iterator& rhs) {return Iterator(lhs-rhs._ptr);}
inline bool operator==(const Iterator& rhs) const {return _ptr == rhs._ptr;}
inline bool operator!=(const Iterator& rhs) const {return _ptr != rhs._ptr;}
inline bool operator>(const Iterator& rhs) const {return _ptr > rhs._ptr;}
inline bool operator<(const Iterator& rhs) const {return _ptr < rhs._ptr;}
inline bool operator>=(const Iterator& rhs) const {return _ptr >= rhs._ptr;}
inline bool operator<=(const Iterator& rhs) const {return _ptr <= rhs._ptr;}
private:
Type* _ptr;
};
Run Code Online (Sandbox Code Playgroud)
总的来说,你的方法是正确的。后缀递增/递减运算符应按值返回,而不是按引用返回。我也有疑问:
Iterator(Type* rhs) : _ptr(rhs) {;}
Run Code Online (Sandbox Code Playgroud)
这告诉大家这个迭代器类是围绕指针实现的。我会尝试使该方法只能由容器调用。对于指针的赋值也是如此。添加两个迭代器对我来说没有意义(我会保留“iterator+int”)。减去指向同一容器的两个迭代器可能有意义。
| 归档时间: |
|
| 查看次数: |
11407 次 |
| 最近记录: |