title: c++ implementing iterators for custom matrix class
body: <p>I am currently working on a linear algebra library (custom vector and matrix plus some algorithms) for educational purposes and personal use. I tried to implement a column iterator, an iterator that traverses a specified column of a Matrix. </p>
  
  <p>Here is a code sample of the vector class (upon which the matrix class is built on):</p>
  
  <pre><code>template&lt;class T&gt;
  class MVector
  {
      std::vector&lt;T&gt; data;
  
  public:
  
      explicit MVector(const std::size_t&amp; n) :data(n) {}
      explicit MVector(std::size_t&amp;&amp; n) :data(n) {}
  
      typename std::vector&lt;T&gt;::iterator Begin(){
          return data.begin();
      }
  
      typename std::vector&lt;T&gt;::iterator End(){
          return data.end();
      }
  
      // many more functions and overloaded operators
      // end of class
  };
  </code></pre>
  
  <p>The matrix class is based on this vector (or the std::vector for this matter), and looks like:</p>
  
  <pre><code>template&lt;class T, std::size_t rowsize, std::size_t colsize&gt;
  class Matrix
  {
  
  private:
  
      // Data is stored in a MVector, a modified std::vector
      MVector&lt;T&gt; matrix;
  
      // size of row dimension of the matrix
      std::size_t row_dim;                  
  
      // size of row dimension of the matrix
      std::size_t column_dim;
  
  public:
  
      Matrix(std::initializer_list&lt;T&gt; il) :matrix(il), 
                                       row_dim(rowsize), column_dim(colsize){}    
      //other constructors...
  
      // iterator
      typename std::vector&lt;T&gt;::iterator Begin(std::size_t row = 0){
          return matrix.Begin()+index(row,0);
      }
  
      typename std::vector&lt;T&gt;::iterator End(std::size_t row = rowsize){
          return matrix.Begin()+index(row,0);
  
      // index (convenience) function to access elements of the matrix via some_matrix(i,j)
      std::size_t index(std::size_t r, std::size_t c) const {
          return r*cols()+c; 
      }
  
      // this is exactly what I want the iterator to do: 
      // only without creating and returning an object. 
  
      // get c'th column
      // slicing is possible from both ends and by "jumping" over elements
      // @ param "begin" - starts at the n'th element 
      // @ param "end" - subtracts m from from the last element.
      // @ param "by" - selects every n'th column 
      MVector&lt;T&gt; get_column(std::size_t c, std::size_t begin = 0, 
                            std::size_t end = 0, std::size_t by = 1) const{
           assert(c &lt; cols() &amp;&amp; end &lt; rows());
           MVector&lt;T&gt; columns;
           for (std::size_t i = index(begin, c); i &lt; index(rows()-end,c); i+=by*cols()) {
               columns.addTo(matrix[i]);
           }
           return columns;                
      } 
  
  // end of class  
  };
  </code></pre>
  
  <p>So, iterating the rows works fine all I have to do is:</p>
  
  <pre><code> int main{
  
   Matrix&lt;int, 3, 2&gt; a = {1,2,3,4,5,6}; 
   for (std::vector&lt;int&gt;::iterator iter = a.Begin(1); iter != a.End(2); iter++) {
       std::cout &lt;&lt; *iter &lt;&lt; " ";
   }
  
   std::cout &lt;&lt; endl;
   return 0;
   }
  
   bash-3.2$ ./main
   3 4 
  </code></pre>
  
  <p>which is exactly what I want. However traversing the columns does not work with that approach. Hence I look for other solutions and found this <a href="https://stackoverflow.com/questions/22480098/designing-iterators-for-a-matrix-class">Designing iterators for a Matrix class</a> article which sounds very similar to my problem and situation, however I could not deduce a solution to the problem. </p>
  
  <p>Other suggestions pointed to the boost libraries iterators: in particular to:     </p>
  
  <pre><code>boost::adaptors::stride(rng, n) 
  boost::adaptors::slice(rng, n, m)
  </code></pre>
  
  <p>which indeed provide very similar results as desired. But so does my get_column function. However I do not want to create a new object. Which is what the boost functions do. From the documentation "Returns: A new range based on rng where traversal is performed in steps of n."</p>
  
  <p>So it seems that the iterator does not know when to stop. </p>
  
  <p>So I am back at square one: how to return am iterator that iterates through the columns of a Matrix that is stored as a vector?</p>