title: Get a reverse iterator from a forward iterator without knowing the value type
body: <p>I'm trying to implement some STL-style sorting algorithms. The prototype for <code>std::sort</code> looks something like this (from <a href="http://cplusplus.com/reference/algorithm/sort/" rel="noreferrer">cplusplus.com</a>):</p>
  
  <pre><code>template &lt;class RandomAccessIterator&gt;
  void sort ( RandomAccessIterator first, RandomAccessIterator last );
  </code></pre>
  
  <p>The function is generally called like this (although the container type can vary):</p>
  
  <pre><code>std::vector&lt;int&gt; myVec;
  // Populate myVec
  std::sort(myVec.begin(), myVec.end());
  </code></pre>
  
  <p>I duplicated the prototype of <code>std::sort</code> for my own sorting function. To iterate through the container to be sorted, I do the following:</p>
  
  <pre><code>template &lt;class RandomAccessIterator&gt;
  void mySort(RandomAccessIterator first, RandomAccessIterator last) {  
    RandomAccessIterator iter;
    for (iter = first; iter != last; ++iter) {
      // Do stuff
    }
  }
  </code></pre>
  
  <p>Easy enough. But what if I want to use a reverse iterator? This would be convenient in algorithms that sort a container from both ends, e.g. <a href="http://en.wikipedia.org/wiki/Cocktail_sort" rel="noreferrer">cocktail sort</a>.</p>
  
  <p>Is there any way to get a reverse iterator from the iterators that are passed in as parameters? If I knew the container type in advance, I could do something like this:</p>
  
  <pre><code>template &lt;class RandomAccessIterator&gt;
  void mySort(RandomAccessIterator first, RandomAccessIterator last) {
    std::vector&lt;int&gt;::reverse_iterator riter(last);
    std::vector&lt;int&gt;::reverse_iterator rend(first);
    for ( ; riter != rend; ++riter) {
      // Do stuff
    }
  }    
  </code></pre>
  
  <p>Unfortunately, I <em>don't</em> know the container type. What I really need to do is something like this:</p>
  
  <pre><code>template &lt;class RandomAccessIterator&gt;
  void mySort(RandomAccessIterator first, RandomAccessIterator last) {
    RandomAccessIterator riter = reverse_iterator(last);
    RandomAccessIterator rend = reverse_iterator(begin);
    for ( ; riter != rend; ++riter) {
      // Do stuff
    }
  }
  </code></pre>
  
  <p>Is there some way to do this without having to pass in reverse iterators as additional parameters (which would solve the problem, but make the function prototype less intuitive)?</p>
  
  <p>Note that I need both forward <em>and</em> reverse iterators in my implementation, so calling the function this way</p>
  
  <pre><code>std::vector&lt;int&gt; myVec;
  // Populate myVec
  mySort(myVec.rbegin(), myVec.rend());
  </code></pre>
  
  <p>will not work.</p>