该接口原理是先实现const_iterator再派生出iterator,并使用静态类型转换去除函数返回值的const属性,思路来自VSC++的标准实现。
其中const_iterator有如下函数缺少定义,需要自己实现,其余函数的定义都是调用以下函数
my_const_iterator() ;//缺 my_const_iterator& operator++();//缺 my_const_iterator& operator--();//缺 reference operator*()const;//缺 pointer operator->()const;//缺 my_const_iterator& operator+=(const difference_type i);//缺 [[nodiscard]] difference_type operator-(const my_const_iterator& other)const;//缺 bool operator==(const my_const_iterator& other)const;//缺 bool operator<(const my_const_iterator& other)const;//缺
双向迭代器以及更低级的迭代器需要修改const_iterator的如下语句并删除部分函数
using iterator_category = std::random_access_iterator_tag;//迭代器类型
完整代码:
#includetemplate class my_const_iterator //容器的const迭代器 { public://类型别名 using iterator_category = std::random_access_iterator_tag;//迭代器类型 using value_type = Type; using difference_type = std::ptrdiff_t;//迭代器距离类型,默认使用std::ptrdiff_t; using pointer = value_type const*; using reference = const value_type&; public: //构造与析构函数 my_const_iterator() ;//缺 ~my_const_iterator() = default; //双向迭代器接口 my_const_iterator& operator++();//缺 my_const_iterator operator++(int){ my_const_iterator tmp = *this;++*this;return tmp;} my_const_iterator& operator--();//缺 my_const_iterator operator--(int) { my_const_iterator tmp = *this; --*this; return tmp; } reference operator*()const;//缺 pointer operator->()const;//缺 my_const_iterator& operator=(const my_const_iterator& other) = default; //随机访问迭代器接口 my_const_iterator& operator+=(const difference_type i);//缺 my_const_iterator& operator-=(const difference_type i) { return *this +=-i} [[nodiscard]] my_const_iterator operator+(const difference_type i)const { my_const_iterator tmp = *this;return tmp+=i; } [[nodiscard]] my_const_iterator operator-(const difference_type i)const { my_const_iterator tmp = *this; return tmp-= i; } [[nodiscard]] difference_type operator-(const my_const_iterator& other)const;//缺 reference operator[](const difference_type i)const { return *(*this + i); } //比较函数 bool operator==(const my_const_iterator& other)const;//缺 bool operator!=(const my_const_iterator& other)const { return !(*this == other); } bool operator<(const my_const_iterator& other)const;//缺 bool operator<=(const my_const_iterator& other)const { return !(*this > other); } bool operator>(const my_const_iterator& other)const { return other > *this }; bool operator>=(const my_const_iterator& other)const { return !(*this < other); } }; template class my_iterator :public my_const_iterator { public://类型别名 using baseIterator = my_const_iterator ; using iterator_category = baseIterator::iterator_category;//迭代器类型 using value_type = Type; using difference_type = typename baseIterator::difference_type; using pointer = value_type*; using reference = value_type&; public: baseIterator::baseIterator;//继承构造函数 ~my_iterator() = default; my_iterator& operator=(const my_iterator& other) = default; //双向迭代器接口 my_iterator& operator++() { baseIterator::operator++(); return *this } my_iterator operator++(int) { my_iterator tmp = *this; baseIterator::operator++(); return tmp; } my_iterator& operator--() { baseIterator::operator--(); return *this } my_iterator operator--(int) { my_iterator tmp = *this; baseIterator::operator--(); return tmp; } reference operator*()const { return const_cast (baseIterator::operator*()); } pointer operator->()const { return const_cast (baseIterator::operator->()); } //随机访问迭代器接口 my_iterator& operator+=(const difference_type i) { baseIterator::operator+=(i); return this } my_iterator& operator-=(const difference_type i) { baseIterator::operator-=(i); return this } [[nodiscard]] my_iterator operator+(const difference_type i)const { my_iterator tmp = *this; return tmp += i; } [[nodiscard]] my_iterator operator-(const difference_type i)const { my_iterator tmp = *this; return tmp -= i; } using baseIterator::operator-; reference operator[](const difference_type i)const { return const_cast ( baseIterator::operator[](i)); }//会覆盖所有的oprator-,所以需要using另一个oprator-使其对外部可见 //比较函数继承自基类 }; //两个逆向迭代器 template using my_const_reverse_iterator = std::reverse_iterator >; template using my_reverse_iterator = std::reverse_iterator >;
水平有限如有错误请指正立即修改



