C ++ const迭代器的指针数组

时间:2018-03-24 10:59:35

标签: c++ arrays pointers iterator

我遇到了const迭代器的问题,因为我尝试迭代一个数组 指针但是当我尝试打印它时,它返回第一个数组 指针指向。我该如何解决?

第一个数组是T的数组,第二个数组是指针数组 到第一个数组:

class const_iterator {
public:
    typedef std::forward_iterator_tag iterator_category;
    typedef T                         value_type;
    typedef ptrdiff_t                 difference_type;
    typedef const T*                  pointer;
    typedef const T&                  reference;

    const_iterator() : ptr(0) { }

    const_iterator(const const_iterator &other) : ptr(other.ptr) { }

    const_iterator& operator=(const const_iterator &other) {
        ptr = other.ptr;
        return *this;
    }

    ~const_iterator() { }

    reference operator*() const {
        return *ptr;
    }

    pointer operator->() const {
        return ptr;
    }

    const_iterator operator++(int) {
        const_iterator tmp(ptr);
        ++ptr;
        return tmp;
    }

    const_iterator& operator++() {
        ++ptr;
        return *this;
    }

    bool operator==(const const_iterator &other) const {
        return other.ptr==ptr;
    }

    bool operator!=(const const_iterator &other) const {
        return other.ptr!=ptr;
    }

private:

    const T *ptr;

    friend class SortedArray; 

    const_iterator(const T*p) : ptr(p){ }

}; 

const_iterator begin() const {
    return const_iterator(*Array);
}

const_iterator end() const {
    return const_iterator(*Array+index);
}

0 个答案:

没有答案