提问人:Bogdan Galatanu 提问时间:5/22/2021 最后编辑:Remy LebeauBogdan Galatanu 更新时间:11/17/2023 访问量:1726
为循环访问指针数组的类创建自定义迭代器
Creating an custom iterator for a class that iterates through an array of pointers
问:
编译器说:
未找到 Array< int 类型的可调用“begin”函数> *
未找到 Array< int 类型的可调用“end”函数> *
它未声明的标识符
在函数中,我尝试使用 .print
for(auto it: this)
我按照本教程创建了一个自定义迭代器,我不知道我做错了什么。
我不太确定我的迭代器结构是否正确定义,因为在他的示例中,他使用了一个简单的整数数组,而我有一个指向类型的指针数组。T
我的问题是,我到底应该编辑什么才能使迭代器正常工作?我想我应该编辑一些类型,但我不太确定要编辑什么。我在底部使用关键字,在函数上。struct Iterator
auto
print
我的类的完整源代码是:
数组.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";
}
主 .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;
}
答:
1赞
Bogdan Galatanu
5/22/2021
#1
通过更改结构中的某些类型来解决。
因为是指针数组,所以它需要具有:It
value_type
应该是 a,因为数组中的每个元素都是 指针T*
pointer
应该是 a,因为它指向指针数组T**
并且应该是 因为是通过 数组中的 pointer 元素。
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";
}
评论
begin
end
Iterator
Iterator*
*this
this