c ++:const_iterator和for-each循环

时间:2014-02-23 05:59:31

标签: c++ vector foreach-loop-container const-iterator

我在C ++中自己实现了Vector。这是一个两部分问题。

第1部分:尝试迭代Vector时,我的begin()和end()迭代器无法处理输入参数。下面是我的整个Vector.h实现:

    class iterator{
    public:

        typedef std::random_access_iterator_tag iterator_category;
        typedef T value_type;

        Vector<T>* ptr;
        uint64_t position;
        COMLINK slave;

        void init(void){
            ptr = nullptr;
            position = 0;
        }
        void copy(const iterator& i){
            init();
            ptr = i.ptr;
            position = i.position;
            slave = i.slave;
        }
        void move(iterator&& i){
            init();
            std::swap(ptr, i.ptr);
            std::swap(position, i.position);
            std::swap(slave, i.slave);
        }

        iterator(void){
            init();
        }
        iterator(const iterator& q){
            copy(q);
        }
        explicit iterator(Vector<T>* v, uint64_t offset){
            init();
            ptr = v;
            position = offset;
            slave = v->master;
        }
        T& operator*(void){
            if ((position >= ptr->len) || ((ptr->buffer + position) < ptr->elem))throw invalid_iterator(invalid_iterator::SEVERE);
            else if (slave.move_cnt != ptr->master.move_cnt) throw invalid_iterator(invalid_iterator::MODERATE);
            else if (slave.alter_cnt != ptr->master.alter_cnt) throw invalid_iterator(invalid_iterator::MILD);
            else if (slave.position_cnt != ptr->master.position_cnt) throw invalid_iterator(invalid_iterator::WARNING);
            return *(ptr->elem + position);
        }
        bool operator==(const iterator& rhs){
            return (ptr == rhs.ptr) && (position == rhs.position) && (slave == rhs.slave);
        }
        bool operator!=(const iterator& rhs){
            return !((*this) == rhs);
        }
        iterator& operator=(const iterator& rhs){
            copy(rhs);
            return *this;
        }
        iterator& operator++(void){
            position++;
            return *this;
        }
        iterator& operator--(void){
            position--;
            return *this;
        }
        iterator operator+(uint64_t i){ // p + i
            iterator temp(*this);
            temp.position += i;
            return temp;
        }
        iterator operator-(uint64_t i){ // p - i
            iterator temp(*this);
            temp.position -= i;
            return temp;
        }
        uint64_t operator-(const iterator& q){
            return position - q.position;
        }
    };

    class const_iterator : public iterator {
    public:
        Vector<T>* const ptr;

        void init(void){
            ptr = nullptr;
            position = 0;
        }
        void copy(const_iterator& i){
            init();
            ptr = i.ptr;
            position = i.position;
            slave = i.slave;
        }
        void copy(iterator& i){
            init();
            ptr = i.ptr;
            position = i.position;
            slave = i.slave;
        }
        void move(const_iterator&& i){
            init();
            std::swap(ptr, i.ptr);
            std::swap(position, i.position);
            std::swap(slave, i.slave);
        }
        void move(iterator&& i){
            init();
            std::swap(ptr, i.ptr);
            std::swap(position, i.position);
            std::swap(slave, i.slave);
        }

        const_iterator(void){
            init();
        }
        const_iterator(const_iterator& i){
            copy(i);
        }
        explicit const_iterator(Vector<T>* const v, uint64_t offset){
            init();
            ptr = v;
            position = offset;
            slave = v->master;
        }
        const_iterator(iterator& i){
            copy(i);
        }
        const T& operator*(void){
            if ((position >= ptr->len) || ((ptr->buffer + position) < ptr->elem))throw invalid_iterator(invalid_iterator::SEVERE);
            else if (slave.move_cnt != ptr->master.move_cnt) throw invalid_iterator(invalid_iterator::MODERATE);
            else if (slave.alter_cnt != ptr->master.alter_cnt) throw invalid_iterator(invalid_iterator::MILD);
            else if (slave.position_cnt != ptr->master.position_cnt) throw invalid_iterator(invalid_iterator::WARNING);
            return *(ptr->elem + position);
        }
        const_iterator& operator=(iterator& i){
            copy(i);
            return *this;
        }
    };

这些是vector中的beign()和end()函数:

    iterator& begin(){
        return iterator(this, 0);
    }

    const_iterator& begin() const{
        return const_iterator(this, 0);
    }

    iterator& end(){
        return iterator(this, len);
    }

    const_iterator& end() const{
        return const_iterator(this, len);
    }

并且......最后(抱歉长度)...这是触发编译错误的测试代码:

const Vector<int32_t>& y = x;
int32_t s = 0;
for (const auto& v : y) {
    s += v;
}

我收到的错误是:

Warning 3   warning C4172: returning address of local variable or temporary c:\users\alexander\documents\visual studio 2013\projects\vectorcontainerphasec_debug\vector.h   319 1   VectorContainerPhaseC_Debug
Error   4   error C2665: 'epl::Vector<int>::const_iterator::const_iterator' : none of the 4 overloads could convert all the argument types  c:\users\alexander\documents\visual studio 2013\projects\vectorcontainerphasec_debug\vector.h   323 1   VectorContainerPhaseC_Debug

我一直在研究这个问题几个小时,但找不到解决方案。有什么提示吗?

第2部分:有没有其他方法来实现const_iterator而不是我上面做的?对于我来说,从迭代器到const_iterator重新定义这么多函数似乎是多余的。我甚至需要创建一个const_iterator类吗?

1 个答案:

答案 0 :(得分:3)

您可能希望const_iterator指向const Vector。您还需要调整构造函数,如下所示:

class const_iterator : public iterator {
public:
    const Vector<T>* const ptr;

    ...
    explicit const_iterator(const Vector<T>* v, uint64_t offset){
        ...
    }

这样,你的const_iterator构造函数应该在你的范围内可调用以进行迭代 如果您需要了解const关键字在指针声明中的工作原理,请参阅Difference between const declarations in C++