循环的2D迭代器



我在实现关于.end((的迭代器类时遇到了问题。我遇到的问题是,当我试图在迭代器类中使用for循环时;它当前刚好在2D向量中的最后一个元素之前停止。但是,我希望它在不引起编译器错误的情况下从最后一个元素开始,并在print语句中包含最后一个字符。

main.cpp

// file contents            
// aaa                          
// bbb                          
// cce 
// factory method (adds file contents to 2D <char> vector)
Base *a = Base::create("file");       
cout << *a; // overloaded '<<'

打印

a a a b b b c c e

现在,当我在迭代器类中使用for循环时,它不包括最后一个字符。

for(auto it = a->begin(); it != a->end(); it++) 
cout << *it << ' ';

打印

a a a b b b c c

.end打印以下

Base::iterator it = aa->end();
cout << *it << 'n';
// prints e

当我尝试while循环时,它包括最后一个字符。

// Note: my iterator class is a nested class inside Base class.
const Base::iterator et = a->begin(); 
int i = 0;
while(i < 13) {
cout << *et << ' ';
et++;
}

打印

a a a b b b c c e e e e e

我知道->end((应该刚好指向最后一个字符,但我不知道如何实现。当我递增超过运算符++(int(中的最后一个值时,它显示一个分段错误。目前,我重载的increment方法在最后一个字符处停止,不会超过它。话虽如此,当从for循环打印时,我如何实现我的++(int(方法以包括最后一个元素?我应该在向量中添加一个null元素还是类似的东西?

Base.cpp 内部

// This function is whats causing the issue. I know it looks ugly.
Base::iterator Base::iterator::operator++(int) {
// base is a Base *, x and y are coordinates for 2D vector
Base::iterator temp(base, x, y); // save value 
// if iterator has not reached end
if( !((x == base->vec.size()-1) && (y == base->vec[0].size()-1)) )
{
// if y < row size
if(y < base->vec[0].size()-1)
y++;
// if y has reached end of row, increment x and start y on a new row
else if(x < base->vec.size() && y == base->vec[0].size()-1) {
y=0;
x++;
}
}
return temp;
}
Base::iterator Base::begin() {
return Base::iterator(this, 0, 0);
}
Base::iterator Base::end() {
return Base::iterator(this, vec.size()-1, vec[0].size()-1);
}

基础的其余部分.cpp

#include "Base.h"
using namespace std;
// Factory method (instantiates 2D vector with file contents)
Base *Base::create(string filename) {/*removed irrelevant code */}
Base::~Base(){}
// prints 2D vector
ostream &operator<<(ostream &os, const Base &val){/*removed irrelevant code*/}
Base::iterator::iterator(Base *b, int m, int n): base(b), x(m), y(n) {}
Base::iterator::~iterator(){}
// returns a character inside 2D vector
char &Base::iterator::operator*() const {
return base->vec[x][y];
}  
bool Base::iterator::operator==(const Base::iterator& rhs) const {
return base->vec[x][y] == *rhs;
}
bool Base::iterator::operator!=(const Base::iterator& rhs) const {
return base->vec[x][y] != *rhs;
}
// Bunch of other functions

如有任何帮助,我们将不胜感激。

Base::iterator(this, vec.size()-1, vec[0].size()-1);这将返回一个有效的最后一个元素。因此,您需要将其更改为Base::iterator(this, vec.size(), 0);,并更新条件以切换到循环中的新行。

类似于:

// if iterator has not reached end
if(x < base->vec.size())
{
++y;
// if y has reached end of row, increment x and start y on a new row
if(y >= base->vec[0].size() {
y=0;
++x;
}
}

迭代器也是错误的:

bool Base::iterator::operator==(const Base::iterator& rhs) const {
return base == rhs.base && x == rhs.x && y == ris.y;
}
bool Base::iterator::operator!=(const Base::iterator& rhs) const {
return !(base == rhs.base && x == rhs.x && y == ris.y);
}