C++实战项目TinySTL之九:reverseIterator.h
程序员文章站
2022-05-24 18:22:04
...
最近看了《STL源码剖析》和侯捷老师的STL课程,打算做一个小型的STL,在增加项目经验的同时也顺带复习STL和数据结构相关的知识。整个系列完全参考岚岚路的博客和github上的一个STL项目项目地址
任务
实现反向迭代器,双向迭代器和支持随机访问的迭代器
#ifndef _REVERSE_ITERATOR_H_
#define _REVERSE_ITERATOR_H_
#include"Iterator.h" //包含之前的Iterator头文件
namespace mySTL {
template<class Iterator>
class reverse_iterator_t {
public:
typedef Iterator iterator_type;
typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
typedef typename iterator_traits<Iterator>::value_type value_type;
typedef typename iterator_traits<Iterator>::difference_type difference_type;
typedef typename iterator_traits<Iterator>::pointer pointer;
typedef const pointer const_pointer;
typedef typename iterator_traits<Iterator>::reference reference;
typedef const reference const_reference;
private:
Iterator base_;
Iterator cur_;
public:
//默认构造函数
reverse_iterator_t():base_(0),cur_(0){}
//explicit阻止隐式的类型转换,此函数只能被显式调用
//构造函数
explicit reverse_iterator_t(const iterator_type& it) : base(it) {
auto temp = it;
cur = --temp;//由于是逆向遍历,所以地址向前移动
}
//拷贝构造函数
template<class Iter>
reverse_iterator_t(const reverse_iterator_t<Iter>& rev_it) {
base_ = (iterator_type)rev_it.base_();
auto temp = base_;
cur = --temp;
}
iterator_type base() { return base_; }//返回反转后的最前端
reference operator*() { return(*cur_); }//返回当前指针
const_reference operator*()const { return (*cur_); }//对常迭代器返回当前指针
pointer operator->() { return &(operator *()); }//返回当前指针的引用
const_pointer operator->(){return &(operator*()) }//返回当前指针的引用
reverse_iterator_t& operator ++() {
--base_;
--cur_;//由于是反向迭代器,所以向前反而是--
return *this;
}
reverse_iterator_t& operator ++(int) {
reverse_iterator_t temp = *this;
++(*this);//对指针本身调用++(其实a++的本质是先保存a为temp然后调用++a最后返回temp)
return temp;//返回之前的指针
}
reverse_iterator_t& operator --() {
++base_;
++cur_;//由于是反向迭代器,所以向前反而是--
return *this;
}
reverse_iterator_t& operator --(int) {
reverse_iterator_t temp = *this;
--(*this);//对指针本身调用++(其实a++的本质是先保存a为temp然后调用++a最后返回temp)
return temp;//返回之前的指针
}
reference operator[](difference_type n) {
return base()[-n - 1];
}
reverse_iterator_t operator +(difference_type n)const;
reverse_iterator_t operator -(difference_type n)const;
reverse_iterator_t& operator +=(difference_type n);
reverse_iterator_t& operator -=(difference_type n);
private:
//对于random_access_iterator和bidirectional_iterator进行额外处理
//random_access_iterator可以直接跳转
Iterator advanceNStep(Iterator it,
difference_type n,
bool right,
random_access_iterator_tag) {
if (right) {
it += n;
}
else {
it -= n;
}
return it;
}
//bidirectional_iterator必须一步一步移动
Iterator advanceNStep(Iterator it,
difference_type n,
bool right,
bidirectional_iterator_tag) {
difference_type i;
difference_type absN = n >= 0 ? n : -n;//绝对值
if ((right&&n>0)||(!right&&n<0) {
for(i=0;i!=absN;++i)
it = it + 1;
}
else if ((!right&&n > 0) || (right&&n < 0){
for (i = 0; i != absN; ++i)
it = it - 1;
}
return it;
}
public:
template <class Iterator>
friend bool operator == (const reverse_iterator_t<Iterator>& lhs,
const reverse_iterator_t<Iterator>& rhs);
template <class Iterator>
friend bool operator != (const reverse_iterator_t<Iterator>& lhs,
const reverse_iterator_t<Iterator>& rhs);
template <class Iterator>
friend bool operator < (const reverse_iterator_t<Iterator>& lhs,
const reverse_iterator_t<Iterator>& rhs);
template <class Iterator>
friend bool operator <= (const reverse_iterator_t<Iterator>& lhs,
const reverse_iterator_t<Iterator>& rhs);
template <class Iterator>
friend bool operator > (const reverse_iterator_t<Iterator>& lhs,
const reverse_iterator_t<Iterator>& rhs);
template <class Iterator>
friend bool operator >= (const reverse_iterator_t<Iterator>& lhs,
const reverse_iterator_t<Iterator>& rhs);
template <class Iterator>
friend reverse_iterator_t<Iterator> operator + (
typename reverse_iterator_t<Iterator>::difference_type n,
const reverse_iterator_t<Iterator>& rev_it);
template <class Iterator>
friend typename reverse_iterator_t<Iterator>::difference_type operator- (
const reverse_iterator_t<Iterator>& lhs,
const reverse_iterator_t<Iterator>& rhs);
};
template<class Iterator>
reverse_iterator_t<Iterator>& reverse_iterator_t<Iterator>::operator+=(difference_type n) {
base_ = advanceNStep(base_, n, false, iterator_category());
cur_ = advanceNStep(cur_, n, false, iterator_category());
return *this;
//调用之前定义的泛型函数实现
}
template<class Iterator>
reverse_iterator_t<Iterator>& reverse_iterator_t<Iterator>::operator-=(difference_type n) {
base_ = advanceNStep(base_, n, true, iterator_category());
cur_ = advanceNStep(cur_, n, true, iterator_category());
return *this;
//调用之前定义的泛型函数实现
}
//+/-调用+=/-=
template<class Iterator>
reverse_iterator_t<Iterator> reverse_iterator_t<Iterator>::operator+(difference_type n)const
{
reverse_iterator_t<Iterator>res = *this;
res += n;
return res;
}
template<class Iterator>
reverse_iterator_t<Iterator> reverse_iterator_t<Iterator>::operator-(difference_type n)const
{
reverse_iterator_t<Iterator>res = *this;
res -= n;
return res;
}
//><==之类的运算符,都是一样的套路
template <class Iterator>
bool operator == (const reverse_iterator_t<Iterator>& lhs, const reverse_iterator_t<Iterator>& rhs) {
return lhs.cur_ == rhs.cur_;
}
template <class Iterator>
bool operator != (const reverse_iterator_t<Iterator>& lhs, const reverse_iterator_t<Iterator>& rhs) {
return !(lhs == rhs);
}
template <class Iterator>
bool operator < (const reverse_iterator_t<Iterator>& lhs, const reverse_iterator_t<Iterator>& rhs) {
return lhs.cur_ < rhs.cur_;
}
template <class Iterator>
bool operator > (const reverse_iterator_t<Iterator>& lhs, const reverse_iterator_t<Iterator>& rhs) {
return lhs.cur_ > rhs.cur_;
}
template <class Iterator>
bool operator >= (const reverse_iterator_t<Iterator>& lhs, const reverse_iterator_t<Iterator>& rhs) {
return !(lhs < rhs);
}
template <class Iterator>
bool operator <= (const reverse_iterator_t<Iterator>& lhs, const reverse_iterator_t<Iterator>& rhs) {
return !(lhs > rhs);
}
//注意由于返回值这里的+-是不对称的!形式有很大区别!
template <class Iterator>
reverse_iterator_t<Iterator> operator+(
typename reverse_iterator_t<Iterator>::difference_type n,
const reverse_iterator_t<Iterator>& rev_it
) {
return rev_it + n;
}
template <class Iterator>
typename reverse_iterator_t<Iterator>::difference_type operator - (
const reverse_iterator_t<Iterator>& lhs,
const reverse_iterator_t<Iterator>& rhs) {
return lhs.cur_ - rhs.cur_;
}
}
#endif // ! _REVERSE_ITERATOR_H
下一篇: ROS教程第五步