c++ 为遍历指针数组的类创建自定义迭代器

yjghlzjz  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(72)

编译器说:
找不到类型Array * 的可调用“开始”函数< int>
未找到类型Array * 的可调用“end”函数< int>
it未声明标识符
print函数中,我尝试使用for(auto it: this)遍历我的指针数组。
我按照this tutorial创建了一个自定义迭代器,我不知道我做错了什么。
我不太确定我的迭代器结构是否定义正确,因为在他的例子中,他使用了一个简单的整数数组,而我有一个指向T类型的指针数组。
我的问题是,我到底应该编辑什么才能让迭代器正常工作?我想我应该编辑struct Iterator中的一些类型,但我不太确定要编辑什么。我在print函数的底部使用了auto关键字。
我的类的完整源代码是:
Array.h

#pragma once
#include<iostream>
using namespace std;
template<class T>
class Array
{
private:

    T** List; // lista cu pointeri la obiecte de tipul T*

    int Capacity; // dimensiunea listei de pointeri

    int Size; // cate elemente sunt in lista

public:
    
    struct Iterator {

        using iterator_category = std::forward_iterator_tag;
        using difference_type = std::ptrdiff_t;
        using value_type = T;
        using pointer = T *;  // or also value_type*
        using reference = T &;  // or also value_type&
        Iterator(pointer ptr) : m_ptr(ptr) {}

        reference operator*() const { return *m_ptr; }
        pointer operator->() { return m_ptr; }

        // Prefix increment
        Iterator& operator++() { m_ptr++; return *this; }

        // Postfix increment
        Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; }

        friend bool operator== (const Iterator& a, const Iterator& b) { return a.m_ptr == b.m_ptr; };
        friend bool operator!= (const Iterator& a, const Iterator& b) { return a.m_ptr != b.m_ptr; };

    private:
        pointer m_ptr;
    };

    Iterator begin() { return Iterator(&List[0]); }
    Iterator end() { return Iterator(&List[Size-1]); }

    Array(); // Lista nu e alocata, Capacity si Size = 0

    ~Array(); // destructor

    Array(int capacity); // Lista e alocata cu 'capacity' elemente

    Array(const Array<T> &otherArray); // constructor de copiere

    T& operator[] (int index); // arunca exceptie daca index este out of range

    const Array<T>& operator+=(T *newElem); // adauga un element de tipul T la sfarsitul listei si returneaza this

    const Array<T>& Insert(int index, const T &newElem); // adauga un element pe pozitia index, retureaza this. Daca index e invalid arunca o exceptie

    const Array<T>& Delete(int index); // sterge un element de pe pozitia index, returneaza this. Daca index e invalid arunca o exceptie

    bool operator=(const Array<T> &otherArray);

    int GetSize();

    int GetCapacity();

    void realocateMemory();

    void printArray();

    bool isIndexValid(int);

};

template<class T>
bool Array<T>::isIndexValid(int index)
{
    //if (index < 0 || index > Size)
    //  throw Exceptions::InvalidIndex;
    return true;
}

template<class T>
void Array<T>::realocateMemory()
{
    T* helper = new T[Size];
    for (int i = 0;i < Size;i++)
        helper[i] = *List[i];

    delete[] List;
    Capacity *= 2;
    List = new T*[Capacity];
    for (int i = 0;i < Size;i++)
        List[i] = new T(helper[i]);
    delete[] helper;
}

template<class T>
int Array<T>::GetSize()
{
    return Size;
}

template<class T>
int Array<T>::GetCapacity()
{
    return Capacity;
}

template<class T>
Array<T>::Array() {
    Capacity = 1;
    Size = 0;
    List = new T*[Capacity];
}

template<class T>
Array<T>::Array(int cap) {
    Capacity = cap;
    List = new T*[Capacity];
}

template<class T>
Array<T>::~Array() {
    Capacity = 0;
    Size = 0;
    delete []List;
}

template<class T>
Array<T>::Array(const Array<T> &otherArray)
{
    delete[]List;
    Size = otherArray.GetSize();
    Capacity = otherArray.GetCapacity();
    List = new T*[Capacity];
    int poz = 0;
    for (auto it : otherArray)
        List[poz++] = it;

}

template<class T>
T& Array<T>::operator[] (int index)
{
    if (!isIndexValid(index))
        throw Exceptions::InvalidIndex;
    return List[index];
}

template<class T>
const Array<T>& Array<T>::operator+=(T *newElem) {
    if (Size == Capacity)
        realocateMemory();
    List[Size++] = newElem;
    return *this;
}

template<class T>
bool Array<T>::operator=(const Array<T> &otherArray)
{
    delete[] List;
    Capacity = otherArray.GetCapacity();
    Size = otherArray.GetSize();
    List = new T*[Capacity];

    for (int i = 0;i < Size;i++)
        List[i] = otherArray[i];    
    return true;
}

template<class T>
const Array<T>& Array<T>::Insert(int index, const T &newElem)
{

    if (Size == Capacity)
        realocateMemory();

    //shift one position to right
    for (int i = Size;i > index;i--)
        List[i] = List[i - 1];

    List[index] = new T(newElem);
    Size++;
    return *this;
}

template<class T>
const Array<T>& Array<T>::Delete(int index)
{
    for (int i = index;i < Size - 1;i++)
        List[i] = List[i + 1];
    Size--;
}

template<class T>
void Array<T>::printArray()
{
    for (int i = 0;i < Size;i++)
        std::cout << *List[i] << ' ';

    cout << "\n---------------------------------------\n";
    for (auto it : this)
        std::cout << *it << ' ';

    cout << "\n---------------------------------------\n";
}

字符串
main.cpp

#include "Array.h"
using namespace std;
#include <vector>
int main()
{

    Array<int>test;

    for (int i = 0;i < 100;i++)
        test += new int(i);
    int x = 444;
    test.Insert(0, x); //add x at index 0

    test.printArray();
    
    
    return 0;
}

zyfwsgd6

zyfwsgd61#

通过改变结构中的一些类型来解决。
因为It是一个指针数组,它需要有:

  • value_type应该是T*,因为数组中的每个元素都是指针
  • pointer应该是T**,因为它指向一个指针数组
  • reference应该是T*&,因为是通过数组中的指针元素进行的引用。

另外,在MatG的帮助下,for(auto it: this)应该改为for(auto it: *this),因为我们需要使用这个类的解引用值。
如果我说错了,请纠正我。

#pragma once
#include<iostream>
using namespace std;
template<class T>
class Array
{
private:

    T** List; // lista cu pointeri la obiecte de tipul T*

    int Capacity; // dimensiunea listei de pointeri

    int Size; // cate elemente sunt in lista

public:
    

    struct Iterator {

        using iterator_category = std::forward_iterator_tag;
        using difference_type = std::ptrdiff_t;
        using value_type = T*;
        using pointer = T **;  // or also value_type*
        using reference = T *&;  // or also value_type&
        Iterator(pointer ptr) : m_ptr(ptr) {}

        reference operator*() const { return *m_ptr; }
        pointer operator->() { return m_ptr; }

        // Prefix increment
        Iterator& operator++() { m_ptr++; return *this; }

        // Postfix increment
        Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; }

        friend bool operator== (const Iterator& a, const Iterator& b) { return a.m_ptr == b.m_ptr; };
        friend bool operator!= (const Iterator& a, const Iterator& b) { return a.m_ptr != b.m_ptr; };

    private:
        pointer m_ptr;
    };

    Iterator begin() { return Iterator(&List[0]); }
    Iterator end() { return Iterator(&List[Size]); }

    Array(); // Lista nu e alocata, Capacity si Size = 0

    ~Array(); // destructor

    Array(int capacity); // Lista e alocata cu 'capacity' elemente

    Array(const Array<T> &otherArray); // constructor de copiere

    T& operator[] (int index); // arunca exceptie daca index este out of range

    const Array<T>& operator+=(T *newElem); // adauga un element de tipul T la sfarsitul listei si returneaza this

    const Array<T>& Insert(int index, const T &newElem); // adauga un element pe pozitia index, retureaza this. Daca index e invalid arunca o exceptie

    const Array<T>& Delete(int index); // sterge un element de pe pozitia index, returneaza this. Daca index e invalid arunca o exceptie

    bool operator=(const Array<T> &otherArray);

    int GetSize();

    int GetCapacity();

    void realocateMemory();

    void printArray();

    bool isIndexValid(int);
};

template<class T>
bool Array<T>::isIndexValid(int index)
{
    //if (index < 0 || index > Size)
    //  throw Exceptions::InvalidIndex;
    return true;
}

template<class T>
void Array<T>::realocateMemory()
{
    T* helper = new T[Size];
    for (int i = 0;i < Size;i++)
        helper[i] = *List[i];

    delete[] List;
    Capacity *= 2;
    List = new T*[Capacity];
    for (int i = 0;i < Size;i++)
        List[i] = new T(helper[i]);
    delete[] helper;
}

template<class T>
int Array<T>::GetSize()
{
    return Size;
}

template<class T>
int Array<T>::GetCapacity()
{
    return Capacity;
}

template<class T>
Array<T>::Array() {
    Capacity = 1;
    Size = 0;
    List = new T*[Capacity];
}

template<class T>
Array<T>::Array(int cap) {
    Capacity = cap;
    List = new T*[Capacity];
}

template<class T>
Array<T>::~Array() {
    Capacity = 0;
    Size = 0;
    delete []List;
}

template<class T>
Array<T>::Array(const Array<T> &otherArray)
{
    delete[]List;
    Size = otherArray.GetSize();
    Capacity = otherArray.GetCapacity();
    List = new T*[Capacity];
    int poz = 0;
    for (auto it : otherArray)
        List[poz++] = it;

}

template<class T>
T& Array<T>::operator[] (int index)
{
    if (!isIndexValid(index))
        throw Exceptions::InvalidIndex;
    return List[index];
}

template<class T>
const Array<T>& Array<T>::operator+=(T *newElem) {
    if (Size == Capacity)
        realocateMemory();
    List[Size++] = newElem;
    return *this;
}

template<class T>
bool Array<T>::operator=(const Array<T> &otherArray)
{
    delete[] List;
    Capacity = otherArray.GetCapacity();
    Size = otherArray.GetSize();
    List = new T*[Capacity];

    for (int i = 0;i < Size;i++)
        List[i] = otherArray[i];    
    return true;
}

template<class T>
const Array<T>& Array<T>::Insert(int index, const T &newElem)
{

    if (Size == Capacity)
        realocateMemory();

    //shift one position to right
    for (int i = Size;i > index;i--)
        List[i] = List[i - 1];

    List[index] = new T(newElem);
    Size++;
    return *this;
}

template<class T>
const Array<T>& Array<T>::Delete(int index)
{
    for (int i = index;i < Size - 1;i++)
        List[i] = List[i + 1];
    Size--;
}

template<class T>
void Array<T>::printArray()
{
    for (int i = 0;i < Size;i++)
        std::cout << *List[i] << ' ';

    cout << "\n---------------------------------------\n";
    for (auto it : *this)
        std::cout <<*it << ' ';

    cout << "\n---------------------------------------\n";
}

字符串

相关问题