samedi 29 juin 2019

Design a simple C++ iterator for Matrix

I was thinking to design a simple C++ iterator with ++ operation behave differently for backward and forward iterations like STL. So that a matrix A can be access through row and column as below,

  A.row(3).begin()
  A.row(3).end()
  A.col(3).begin()
  A.col(3).end()
  A.col(3).rbegin()
  A.col(3).rend()

  ++ A.row(3).begin()
  ++ A.col(3).rbegin()

My matrix class would look like below,

class Matrix {
 public:
  Iter row(size_t rowID);
  Iter col(size_t colID);
 private:
  vector<int> data_{1, 2, 3, 4, 5, 6};
  size_t nRow{3};
  size_t nCol{2};
};

Is there any suggestions on how I can design my Iter class?

Aucun commentaire:

Enregistrer un commentaire