title: iterator can not stop at the end(c++)
body: <p>What I want to do is to find the kth to last element of a single linked list. I have the recursive solution below:
  In the main() function, I create a list holding number 10 to 1</p>
  
  <pre><code>int main()
  {
  int i;
  forward_list&lt;int&gt; flist;
  for (i = 1; i &lt;= 10; i ++)
      flist.push_front(i);
  
  forward_list&lt;int&gt;::iterator iter;
  cout &lt;&lt; "Original Sequence:" &lt;&lt; endl;
  for (iter = flist.begin(); iter != flist.end(); iter ++)
      cout &lt;&lt; *iter &lt;&lt; " ";
  cout &lt;&lt; endl;
  
  forward_list&lt;int&gt;::iterator end = flist.end();
  forward_list&lt;int&gt;::iterator begin = flist.begin();
  forward_list&lt;int&gt;::iterator ret;
  
  ret = test_func(begin, end);
  cout &lt;&lt; "The " &lt;&lt; TARGET &lt;&lt; "th(st,nd,rd) to last element is " &lt;&lt; *ret &lt;&lt; endl;
  return 0;   
  }
  </code></pre>
  
  <p>After I created the list, I call test_func. The return value should be the iterator of the element I want.</p>
  
  <pre><code>#define TARGET 3
  static int counttt = 0;
  
  forward_list&lt;int&gt;::iterator test_func(forward_list&lt;int&gt;::iterator iter, forward_list&lt;int&gt;::iterator end)
  {
  
  cout &lt;&lt; "test..." &lt;&lt; endl;
  
  forward_list&lt;int&gt;::iterator temp;
  if ((iter ++) != end)
      temp = test_func(iter, end);
  counttt ++;
  if (counttt &lt; TARGET)
      return end;
  else if (counttt == TARGET)
      return iter;
  else
      return temp;
  }
  </code></pre>
  
  <p>The "test..." is for debugging which tells me how many times the test_func() has been called. Here, in my opinion, test_func() should be called 10 times. However, it will be call for 11 times and the last time will cause a segmentation fault.I feel like ((iter ++) != end) does not happen at the right time.</p>
  
  <p>All Codes are shown below:</p>
  
  <pre><code>#include &lt;forward_list&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  #include &lt;stdlib.h&gt;
  
  using namespace std;
  
  #define TARGET 3
  static int counttt = 0;
  
  forward_list&lt;int&gt;::iterator test_func(forward_list&lt;int&gt;::iterator iter, forward_list&lt;int&gt;::iterator end)
  {
  
  cout &lt;&lt; "test..." &lt;&lt; endl;
  
  forward_list&lt;int&gt;::iterator temp;
  if ((iter ++) != end)
      temp = test_func(iter, end);
  counttt ++;
  if (counttt &lt; TARGET)
      return end;
  else if (counttt == TARGET)
      return iter;
  else
      return temp;
  }
  
  int main()
  {
  int i;
  forward_list&lt;int&gt; flist;
  for (i = 1; i &lt;= 10; i ++)
      flist.push_front(i);
  
  forward_list&lt;int&gt;::iterator iter;
  cout &lt;&lt; "Original Sequence:" &lt;&lt; endl;
  for (iter = flist.begin(); iter != flist.end(); iter ++)
      cout &lt;&lt; *iter &lt;&lt; " ";
  cout &lt;&lt; endl;
  
  forward_list&lt;int&gt;::iterator end = flist.end();
  forward_list&lt;int&gt;::iterator begin = flist.begin();
  forward_list&lt;int&gt;::iterator ret;
  
  ret = test_func(begin, end);
  cout &lt;&lt; "The " &lt;&lt; TARGET &lt;&lt; "th(st,nd,rd) to last element is " &lt;&lt; *ret &lt;&lt; endl;
  return 0;   
  }
  </code></pre>
  
  <p>Thanks,</p>
  
  <p>Kevin Zhou</p>