如何保护我的迭代器,以便它引发异常,如果它指向.end()

时间:2017-03-20 18:42:35

标签: c++

我写了这个Vector课程和一个嵌套的Iterator课程和一个const_Iterator课程,现在我必须保护Vector这样我的Iterator没有使用例如我的.end()方法指出超出operator ++并且如果我尝试则抛出异常。 所以在我的iterator课程中,我无法访问.end(),因为它是一个矢量方法。所以我在思考,而不是指向我的Iterator课程中的值,而是指向整个向量,但我无法使用.end()访问Vector*。 我是否正确地找到解决方案,如果我是如何通过我的Vector,那么我可以按照我打算的方式使用它?

class Vector{
      public:
        using value_type= double;
        using size_type= size_t;
        using difference_type= ptrdiff_t;
        using reference = double&;
        using const_reference= const double&;
        using pointer = double*;
        using const_pointer= const double*;
        using iterator = double*;
        using const_iterator= const double*;
    private:
     size_t sz;
     size_t max_sz;
     double* values=nullptr;
    class const_Iterator{
            public:
                using value_type = double;
                using difference_type = ptrdiff_t;
                using reference = double&;
                using pointer = double*;
                using iterator_category = std::forward_iterator_tag;
            private:
            double* ptr;
            size_t cnt;
            public:
                const_Iterator(double* p){
                    ptr=p;
                    cnt=0;

                 }
                 const_Iterator& operator++ () {
                    ptr++;
                    cnt = 0;
                    return *this;
                }
                bool  operator==(const const_Iterator& rop)const {
                    return this->ptr == rop.ptr;
                }

                bool  operator!=(const const_Iterator& rop)const {
                    return this->ptr != rop.ptr;
                }
                const double operator* () const {
                    return *ptr;
                }
                friend Vector::difference_type operator-(const Vector::const_Iterator& lop,const Vector::const_Iterator& rop) {
                    return lop.ptr-rop.ptr;
                }
        };
            class Iterator{
            public: 
                using value_type = double;
                using difference_type = ptrdiff_t;
                using reference = double&;
                using pointer = double*;
                using iterator_category = std::forward_iterator_tag;
            private:
            double* ptr;
            size_t cnt;
            public:
                Iterator(double* p){
                    ptr=p;
                    cnt=0;

                 }
                 Iterator& operator++() {
                    ptr++;
                    cnt = 0;
                    return *this;
                }
                Iterator operator++(int){
                    Iterator a(ptr);
                    ptr++;
                    return a;
                }
                bool operator==( Iterator& rop) {
                    return this->ptr != rop.ptr;
                }

                bool operator!=(const Iterator& rop) {
                    return this->ptr != rop.ptr;
                }
                double& operator*() {
                    return *ptr;
                }
                 operator const_Iterator() const{

                 return const_Iterator(ptr);
                 }; 



                 };
const_Iterator end() const{return const_Iterator(values+sz);}    
const_Iterator  begin() const{return const_Iterator(values);}
   Iterator begin()  { return values; }
   Iterator end()  { return values + sz; }
        size_t min_sz = 5;
        Vector();
        Vector(size_t);
        Vector(const Vector&);
        Vector (initializer_list<double> ); 
        void push_back(double);
        void reserve(size_t);
        void pop_back();
        bool empty();
        void clear();
        Vector&  operator=(const Vector&);
        const double& operator[] (size_t) const;
        double& operator[] (size_t) ;

        void fit_to_shrink();
        size_t size()const {return sz;}
        ostream& print(ostream&) const;
};  

1 个答案:

答案 0 :(得分:1)

您的迭代器可能如下所示:

class Iterator{
public:
    // ... using type

private:
    Vector* parent;
    std::size_t index;
public:
    Iterator(Vector& v, std::size_t index) : ptr(&v), index(index) {}

     Iterator& operator++() {
       if (index == parent->size()) {
           throw std::runtime_error("++ on end iterator");
       }
        ++index;
        return *this;
    }

    Iterator operator++(int){
        Iterator old(*this);
        ++(*this);
        return old;
    }

    bool operator==(const Iterator& rhs) const {
        if (parent != rhs.parent) {
            throw std::runtime_error("You compare iterator of different containers");
        }
        return index == rhs.index;
    }

    bool operator!=(const Iterator& rop) const { return !(*this == rhs); }

    double& operator*() { return parent->at(index); } // `at` throws on invalid index

    // ...

 };

你的矢量:

Iterator Vector::begin() { return Iterator(this, 0);}
Iterator Vector::end() { return Iterator(this, size());}