c++ conditional uni-directional iterator : <p>I want to achieve something like the pseudo-code below:</p>

<pre><code>string foo;  // or vector&lt;int&gt; foo;
auto itr = bar?  foo.begin() : foo.rbegin();
auto end = bar?  foo.end() : foo.rend();
for (  ; itr != end; ++itr) {
// SomeAction ...
}
</code></pre>

<p>That is, I want to set <code>itr</code> to be either a forward iterator or the reverse iterator, depending on some condition <code>bar</code>, to scan in forward or reverse direction.</p>

<p>Apparently code like that won't work, since forward iterator and reverse iterator has different type.</p>

<p>Note that I don't want to split into two loops, as those code like <code>// SomeAction</code> will be duplicated.</p>

<p>How can I do that?
Answers using C++11 and/or before are preferred.</p>

<p>Also, please elaborate if string and vector have different solutions.</p>
