title: Implement custom iterator for c++ std container
body: <p>I am trying to write a custom iterator for my Vector/Matrix class. The purpose of the iterator is to iterate through a std::vector and skip every n'th element. I already asked a similar question <a href="https://stackoverflow.com/questions/26282847/c-implementing-iterators-for-custom-matrix-class/26296065#26296065">c++ implementing iterators for custom matrix class</a> which indeed provides a solution to the problem. </p>
  
  <p>However this approach is too slow. I did quite a bit of reading: Effective C++, C++ Primer etc. and on-line resources: <a href="http://www.drdobbs.com/stl-generic-programming-writing-your-ow/184401417" rel="nofollow noreferrer">http://www.drdobbs.com/stl-generic-programming-writing-your-ow/184401417</a> or <a href="https://stackoverflow.com/questions/3582608/how-to-correctly-implement-custom-iterators-and-const-iterators">How to correctly implement custom iterators and const_iterators?</a> and of course: <a href="http://www.cplusplus.com/reference/iterator/iterator/" rel="nofollow noreferrer">http://www.cplusplus.com/reference/iterator/iterator/</a>.</p>
  
  <p>The last one in particular provides a working code. However I can't really figure out what the code does and how to implement this for a std::vector. Here is the code: </p>
  
  <pre><code>template&lt;class T&gt;
  class iter: public std::iterator&lt;std::random_access_iterator_tag, T&gt;{
  private:
      T* m_pter;
  
  public:
  
      iter(T* value):  m_pter(value){}
      iter(const iter&amp; other_it): m_pter(other_it.m_pter){}
      iter&amp; operator++() { ++m_pter; return *this; }
      bool operator!=(const iter&amp; rhs) {return m_pter!=rhs.m_pter;}
      T&amp; operator*() { return *m_pter; }
  
  };
  </code></pre>
  
  <p>and the main:</p>
  
  <pre><code>int main(int argc, char *argv[]){
  
      int a[]  = {1,2,3,4,5,6};
      iter&lt;int&gt; from(a);
      iter&lt;int&gt; to(a+5);
      for (iter&lt;int&gt; iter = from; iter != to; ++iter) {
          std::cout &lt;&lt; *iter &lt;&lt; std::endl;
      }
  
      // std::vector&lt;int&gt; a  = {1,2,3,4,5,6};
      // iter&lt;std::vector&lt;int&gt; &gt; from(a);
      // iter&lt;std::vector&lt;int&gt; &gt; to(a+5);
      // for (iter&lt;std::vectorVECTOR&lt;int&gt; &gt; iter=from; iter!=to; ++iter) {
      //     std::cout &lt;&lt; *iter &lt;&lt; std::endl;
      // }
  
  }
  </code></pre>
  
  <p>The upper part in the main function returns:
      $ ./main
      1 2 3 4 5</p>
  
  <p>and the lower part:</p>
  
  <pre><code>main.cpp:49:21: error: no matching function for call to ‘iter&lt;int&gt;::iter(std::vector&lt;int&gt;&amp;)’
       iter&lt;int&gt; from(a);
  </code></pre>
  
  <p>I would really like to understand why the code works for the array but not the vector. What would I need to change in order for it to work for the vector?</p>
  
  <p>Your comments are greatly appreciated.</p>
  
  <p>Vincent</p>