c++ - 自定义 C++ 迭代器问题

标签 c++ vector iterator

我正在尝试实现我自己的 Iterator 类,但在使用 end 函数(指向 Iterator 的末尾)时遇到了问题。以下是 .h 文件中的大部分实现:

template <typename ItemType>
class myArray
{
private:
    ItemType* arrayData; // pointer to ALL THE DATA
    static const size_t INITIAL_CAPACITY = 1;
    size_t current_capacity;
    size_t num_items; // number of items in the array being held
public:
    myArray <ItemType>(const int array_size = INITIAL_CAPACITY) { // constructor
        current_capacity = array_size;
        num_items = 0;
        arrayData = (ItemType*)malloc(sizeof(ItemType) * array_size);
        if (arrayData == NULL) {
            std::cout << "Error allocating memory" << std::endl;
            throw arrayData;
        }
        memset(arrayData, 0, sizeof(ItemType) * array_size);
    }
    class Iterator {
    private:
        myArray* marker; //points to my array
        int index; // location in my array
        int mode;
    public:
        Iterator(myArray* vect, int index = 0, int mode = 0);
    };

    Iterator begin() {      
        return Iterator(arrayData, 0, 0);
    }
    Iterator end() {
        return Iterator(std::begin(myArray& arrayData), num_items, 0);
    } // last of array 
};

每当我编译它只是为了构建它时,我都会收到以下错误:

<function-style-cast>': cannot convert from 'int *' to 'myArray<int>::Iterator

我很确定问题出在构造函数中,或者出在我如何调用 Iterator 的构造函数中,但我似乎无法弄清楚。

这是构造函数:

template<typename ItemType>
myArray<ItemType>::Iterator::Iterator(myArray* vect, int index, int mode)
{
    this->marker = (ItemType*)malloc(sizeof(ItemType)) * vect;
    if (marker == NULL) {
        std::cout << "Error allocating memory" << std::endl;
        throw marker;
    }
    memset(marker, 0, sizeof(ItemType) * vect);
    this->index = index;
    this->mode = mode;
}

============================================= ==============

我已经尝试将构造函数更改为:

template<typename ItemType>
myArray<ItemType>::Iterator::Iterator(myArray* vect, int index, int mode)
{
    this->marker = vect;
    this->index = index;
    this->mode = mode;
}

以及随后的结束调用:

Iterator end() {
    return Iterator(arrayData + num_items); //This should give me the end of the array right?
} 

这样做时我得到了一个不同的错误,所以我不确定出了什么问题。我对构造函数的实现是正确的,还是在我的代码中的其他地方?我认为这与我调用构造函数的方式有关。任何帮助,将不胜感激。我能找到的关于这方面的帖子都对我没有帮助,所以我转向了你们。

最佳答案

一些让您继续前进的修复:

template <typename ItemType>
class myArray
{
private:
    ItemType* arrayData; // pointer to ALL THE DATA
    static const size_t INITIAL_CAPACITY = 1;
    size_t current_capacity;
    size_t num_items; // number of items in the array being held
public:
    myArray(const int array_size = INITIAL_CAPACITY) {
        current_capacity = array_size;
        num_items = 0;
        arrayData = new ItemType[array_size](); // Allocate. And value/zero-initialialize, note () at the end.
    }
    class Iterator {
    private:
        ItemType* element_; // Points to the current element.
    public:
        Iterator(ItemType* element)
            : element_(element)
        {}
    };

    Iterator begin() {
        return Iterator(arrayData);
    }
    Iterator end() {
        return Iterator(arrayData + num_items);
    }
};

注意事项:

  • 内部myArray类模板,myArraymyArray<ItemType> , 无需指定模板参数。
  • 使用new而不是 malloc . new为您初始化元素并抛出失败。
  • 您可能希望您的类(class)名称以大写字母开头;以及以小写字母开头的变量和函数。这是 C++ 和其他语言中非常常见的约定。这有助于阅读代码。

您需要通过添加析构函数、复制构造函数和赋值以及实现迭代器来完成此类。

关于c++ - 自定义 C++ 迭代器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48695904/

相关文章:

c++ - fprintf 对函数返回的字符有问题

c++ - GStreamer 插件与 GStreamermm(C++)

vector 数组的 C++ 实现

c++ - 前向声明的问题 - 友元函数和线/点类

c++ - 创建 vector <thread>

r - 生成从结束到开始顺序配对值的不同长度向量

c++ - 使用自定义分配器分配 std::vector 并且在运行时类型未知

c++ - 将几个 std::list 迭代器压缩在一起

python - 如何将两个非常大的字符串压缩在一起并返回匹配和不匹配的索引?

java - HashMap 中的 ConcurrentModificationException