2D循环迭代器

时间:2018-11-20 22:25:21

标签: c++ iterator

我在实现有关.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

我知道a-> end()应该指向最后一个字符,但是我不知道如何实现。当我超过操作符++(int)中的最后一个值时,它将显示分段错误。目前,我的重载增量方法在最后一个字符处停止,并且没有越过它。这样说,从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);
}

Base.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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min 1 0 0.2 0.2 0.2 0.2 0.2 0.2 2 1000 0.6 0.7 0.5 0.1 0.3 0.1 1 0 0.4 0.4 0.4 0.4 0.4 0.4 1 0 0.5 0.5 0.5 0.5 0.5 0.5 3 14000 0.8 0.8 0.8 0.6 0.6 0.6 ,这将返回有效的最后一个元素。因此,您需要将其更改为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;
  }
}