【问题标题】:Use iterators with custom-made vector in C++在 C++ 中使用带有自定义向量的迭代器
【发布时间】:2020-03-08 15:00:50
【问题描述】:

我创建了一个自定义向量,我想为其创建一个迭代器,我已将代码简化为一个极简版本,只剩下一个错误。我想修复这个错误,然后让代码做它应该做的事情,即打印数组元素的总和。

代码如下:

#include <iostream>
using namespace std;



template <class Elem>
class ArrayVector{
    public :
        ArrayVector(){
            A = new Elem[3];
            A[0] = 1;
            A[1] = 2;
            A[3] = 3;
            ptr = &A[0];
        }
        const ArrayVector * begin(){
            this->begin();
        }
    class iteratorArray{
            Elem * current;
            const ArrayVector * begin(){
                return iteratorArray();
            }
            const ArrayVector* end(){  
                iteratorArray* end = new iteratorArray(&current[3]);
                return end;
            }
            iteratorArray(Elem* initLoc){
                current = initLoc;
            }
            iteratorArray operator+(int n){
                current = &current[n];
                return *this;
            }
            bool operator!=(iteratorArray& obj2){
                return this->current != obj2.current;
            }
            iteratorArray operator++(){
                current = current+1;
                return *this;
            }
            Elem& operator*(){
                return *current;
            }
    };
    Elem* A;
    Elem* ptr;
};


int main() {
    ArrayVector<int> vector1;
    typedef ArrayVector<int>::iteratorArray iteratorArray;  
    int sum = 0;
    for (iteratorArray p = vector1.begin(); p != vector1.end(); ++p)
        sum += *p;
    return sum;
}

【问题讨论】:

  • 您的 ArrayVector 没有任何 beginend 函数 - 您将它们定义为 iteratorArray 的成员
  • 由于您是新来的,请拨打tour并阅读How to Ask。对于像您这样的问题,还需要提取并提供minimal reproducible example。另外,你犯了一个错误,你咬得太多而无法咀嚼。取而代之的是更小的增量步骤,例如放弃整个template ... 的东西。此外,为了跟踪您的进度,请考虑“测试驱动开发”(在网上搜索!),它是许多编程任务的宝贵技术。
  • 为什么delete [] A; 在你的ArrayVector 复制构造函数中?对象是全新的,delete 有什么用?
  • 您的begin 函数缺少return,甚至没有声明返回迭代器,而是指向ArrayVector 的指针
  • 我的大学作业需要使用迭代器

标签: c++ arrays vector iterator


【解决方案1】:

你实现了你的iteratorArray 都错了。它应该看起来更像这样:

#include <iostream>

template <class Elem>
class ArrayVector{
private:
    Elem* A;

public :
    ArrayVector(){
        A = new Elem[3];
        A[0] = 1;
        A[1] = 2;
        A[2] = 3;
    }

    ArrayVector(const ArrayVector &src){
        A = new Elem[3];
        A[0] = src.A[0];
        A[1] = src.A[1];
        A[2] = src.A[2];
    }

    ~ArrayVector(){
        delete[] A;
    }

    ArrayVector& operator=(const ArrayVector &rhs){
        A[0] = rhs.A[0];
        A[1] = rhs.A[1];
        A[2] = rhs.A[2];
        return *this;
    }

    class iterator {
    private:
        Elem * current;

    public:
        iterator(Elem* initLoc){
            current = initLoc;
        }

        iterator operator+(int n) const {
            return iterator(current + n);
        }

        iterator operator-(int n) const {
            return iterator(current - n);
        }

        iterator& operator++(){
            ++current;
            return *this;
        }

        iterator operator++(int){
            return iterator(current++);
        }

        iterator& operator--(){
            --current;
            return *this;
        }

        iterator operator--(int){
            return iterator(current--);
        }

        iterator& operator+=(int n){
            current += n;
            return *this;
        }

        iterator& operator-=(int n){
            current -= n;
            return *this;
        }

        bool operator!=(const iterator& rhs) const{
            return current != rhs.current;
        }

        Elem& operator*(){
            return *current;
        }

        // and so on for other operators that a RandomAccess iterator needs to implement...
    };

    iterator begin(){
        return iterator(A);
    }

    iterator end(){
        return iterator(A+3);
    }
};

int main() {
    ArrayVector<int> vector1;
    typedef ArrayVector<int>::iterator iteratorArray;  
    int sum = 0;
    for (iteratorArray p = vector1.begin(); p != vector1.end(); ++p)
        sum += *p;
    std::cout << sum;
    return 0;
}

Live Demo

也就是说,您根本不需要自定义 iterator。您可以只使用原始指针,它们是完全有效的迭代器。 ArrayVector::begin() 可以返回AArrayVector::end() 可以返回A+3

#include <iostream>

template <class Elem>
class ArrayVector{
private:
    Elem* A;

public :
    ArrayVector(){
        A = new Elem[3];
        A[0] = 1;
        A[1] = 2;
        A[2] = 3;
    }

    ArrayVector(const ArrayVector &src){
        A = new Elem[3];
        A[0] = src.A[0];
        A[1] = src.A[1];
        A[2] = src.A[2];
    }

    ~ArrayVector(){
        delete[] A;
    }

    ArrayVector& operator=(const ArrayVector &rhs){
        A[0] = rhs.A[0];
        A[1] = rhs.A[1];
        A[2] = rhs.A[2];
        return *this;
    }

    typedef Elem* iterator;

    iterator begin(){
        return A;
    }

    iterator end(){
        return A+3;
    }
};

int main() {
    ArrayVector<int> vector1;
    typedef ArrayVector<int>::iterator iteratorArray;  
    int sum = 0;
    for (iteratorArray p = vector1.begin(); p != vector1.end(); ++p)
        sum += *p;
    std::cout << sum;
    return 0;
}

Live Demo

【讨论】:

    【解决方案2】:

    这是您程序的一个版本,已更改到足以正常运行。我将迭代器移出并使其成为模板,因为它并不真正依赖于ArrayVector。迭代器是按值复制的。你可以看到一个工作版本here

    #include <iostream>
    using namespace std;
    
    template <class Elem>
    class iteratorArray{
        Elem * current;
    public:
        iteratorArray(Elem* initLoc){
            current = initLoc;
        }
        iteratorArray operator+(int n){
            current = &current[n];
            return *this;
        }
        bool operator!=(iteratorArray const& obj2){
            return this->current != obj2.current;
        }
        iteratorArray operator++(){
            current = current+1;
            return *this;
        }
        Elem& operator*(){
            return *current;
        }
    };
    
    template <class Elem>
    class ArrayVector{
    public :
        ArrayVector(){
            A = new Elem[3];
            A[0] = 1;
            A[1] = 2;
            A[2] = 3;
            ptr = &A[0];
        }
    
        const iteratorArray<Elem> begin(){
            return iteratorArray<Elem>(ptr);
        }
        const iteratorArray<Elem> end(){  
            return  iteratorArray<Elem>(ptr + 3);
        }
    
        Elem* A;
        Elem* ptr;
    };
    

    【讨论】:

      猜你喜欢
      • 2010-10-24
      • 1970-01-01
      • 2015-11-19
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      相关资源
      最近更新 更多