迭代器iterator接口

1 篇文章 0 订阅

STL迭代器iterator接口

该接口原理是先实现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;//迭代器类型

完整代码:

#include<iterator>
template<typename Type>
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<typename Type>
class my_iterator :public my_const_iterator<Type> {
public://类型别名
    using BaseIterator = my_const_iterator<Type>;

    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<reference>(BaseIterator::operator*()); }
    pointer operator->()const { return const_cast<pointer>(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<reference>( BaseIterator::operator[](i)); }//会覆盖所有的oprator-,所以需要using另一个oprator-使其对外部可见
    

    //比较函数继承自基类
};
//两个逆向迭代器
template<typename Type>
using my_const_reverse_iterator = std::reverse_iterator<my_const_iterator<Type>>;
template<typename Type>
using my_reverse_iterator = std::reverse_iterator<my_iterator<Type>>;

水平有限如有错误请指正立即修改

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值