Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;基于现有随机访问迭代器的反向迭代器?_C++_Iterator - Fatal编程技术网

C++ C++;基于现有随机访问迭代器的反向迭代器?

C++ C++;基于现有随机访问迭代器的反向迭代器?,c++,iterator,C++,Iterator,通过继承非常基本的std::iterator模板类(见下文),我为我的自定义容器类实现了一个(非常量)随机访问迭代器。要做到这一点,只需传入所需的类型 关于如何设置反向迭代器,我还没有找到太多的信息,但我非常确定有一种方法可以将现有迭代器与一些新的typedef一起使用,并定义rbegin()和rend()。这就是我被困的地方。有人能帮忙吗 template <class T, class A = std::allocator<T>> class ring { publi

通过继承非常基本的std::iterator模板类(见下文),我为我的自定义容器类实现了一个(非常量)随机访问迭代器。要做到这一点,只需传入所需的类型

关于如何设置反向迭代器,我还没有找到太多的信息,但我非常确定有一种方法可以将现有迭代器与一些新的typedef一起使用,并定义rbegin()和rend()。这就是我被困的地方。有人能帮忙吗

template <class T, class A = std::allocator<T>>
class ring {
public:
    typedef ring<T, A> self_type;
    typedef T value_type;
    typedef A alloc_type;
    typedef ptrdiff_t size_type;
    typedef typename alloc_type::difference_type    difference_type;
    typedef typename alloc_type::pointer            pointer;
    typedef typename alloc_type::reference          reference;
    typedef typename alloc_type::const_pointer      const_pointer;
    typedef typename alloc_type::const_reference    const_reference;

    class iterator;  // we implement iterator as nested class, so no need to typedef it

    iterator begin() { return iterator(this, 0); }
    iterator end() { return iterator(this, size()); }  // the current size (which is one past the end of the array)

    class iterator : public std::iterator<std::random_access_iterator_tag, value_type, difference_type, pointer, reference> {
    private:
        self_type *ring_;  // typedefs defined in ring class apply here
        size_type offset_;
    public:
        iterator(self_type *r, size_type o) : ring_{r}, offset_{o} {}
        reference operator* () const { return (*ring_)[offset_]; }
        pointer operator-> () const { return &(operator*()); }
        iterator& operator++ () {
            ++offset_;
            return *this;
        }
        friend bool operator== (const iterator& it1, const iterator& it2) { return ((it1.ring_ == it2.ring_ && it1.offset_ == it2.offset_)); }
        friend bool operator!= (const iterator& it1, const iterator& it2) { return (!(it1 == it2)); }
        iterator& operator-- () {
            --offset_;
            return *this;
        }
        iterator operator++ (int) {
            iterator clone(*this);  // make a duplicate
            ++offset_;
            return clone;  // return the duplicate
        }
        iterator operator-- (int) {  // has to be return by value
            iterator clone(*this);
            --offset_;
            return clone;
        }
        iterator& operator+=(size_type n) {
            offset_ += n;
            return *this;
        }       
        iterator& operator-=(size_type n) {
            offset_ -= n;
            return *this;
        }
        ...
        reference operator[] (size_type n) const { return (*ring_)[n]; }
    };
};
模板
阶级戒指{
公众:
typedef环自_型;
类型定义T值_类型;
类型定义为alloc_类型;
typedef ptrdiff_t size_type;
typedef typename alloc_type::difference_type difference_type;
typedef typename alloc_type::指针;
typedef typename alloc_type::reference;
typedef typename alloc_type::常量指针常量指针;
typedef typename alloc_type::const_reference const_reference;
类迭代器;//我们将迭代器实现为嵌套类,因此无需对其进行类型定义
迭代器begin(){返回迭代器(this,0);}
迭代器end(){return iterator(this,size());}//当前大小(超过数组末尾一个)
类迭代器:public std::iterator{
私人:
self_type*ring;//在ring类中定义的typedef在此应用
尺寸\类型偏移量\;
公众:
迭代器(self_-type*r,size_-type o):环{r},偏移量{o}
引用运算符*()常量{return(*ring_uu)[offset_u];}
指针运算符->()常量{return&(operator*());}
迭代器和运算符++(){
++偏移量;
归还*这个;
}
friend bool运算符==(常量迭代器&it1,常量迭代器&it2){return((it1.ring==it2.ring&&it1.offset==it2.offset))}
友元布尔运算符!=(常量迭代器&it1,常量迭代器&it2){return(!(it1==it2));}
迭代器和运算符--(){
--偏移量;
归还*这个;
}
迭代器运算符++(int){
迭代器克隆(*this);//复制
++偏移量;
return clone;//返回副本
}
迭代器运算符--(int){//必须按值返回
迭代器克隆(*this);
--偏移量;
返回克隆;
}
迭代器和运算符+=(大小\u类型n){
偏移量μ+=n;
归还*这个;
}       
迭代器和运算符-=(大小\u类型n){
偏移量\-=n;
归还*这个;
}
...
引用运算符[](size_type n)常量{return(*ring_u)[n];}
};
};

您需要定义
反向迭代器
类型别名以及
rbegin
rend
方法。反向迭代器应使用
std::reverse_iterator
(在标题
中定义)实现

使用reverse_iterator=std::reverse_iterator;
使用const_reverse_iterator=std::reverse_iterator;
反向迭代器rbegin(){return end();}
const_reverse_迭代器rbegin()const{return end();}
反向迭代器rend(){return begin();}
const_reverse_迭代器rend()const{return begin();}
const_reverse_迭代器crbegin()const{return rbegin();}
const_reverse_迭代器crend()const{return rend();}

这就是一切。没有魔法。

你熟悉吗?在任何情况下,您都需要自己实现
rbegin()
rend()
iterator
类模板在C++17中不受欢迎,因为它容易出错。是的,我听说它是一种不受欢迎的方法。最佳实践方案是什么?@sid自己声明类型。或者推出自己的版本。(顺便说一句,它是一个类模板,不是一个方法。)
using       reverse_iterator = std::reverse_iterator<      iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;

      reverse_iterator rbegin()       { return end  (); }
const_reverse_iterator rbegin() const { return end  (); }
      reverse_iterator rend  ()       { return begin(); }
const_reverse_iterator rend  () const { return begin(); }

const_reverse_iterator crbegin() const { return rbegin(); }
const_reverse_iterator crend  () const { return rend  (); }