我从来没有实现类似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 …Run Code Online (Sandbox Code Playgroud)