Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>std::for_each expects a unary function (or at least something with the typedefs of a unary function). </p> <p>std::reverse&lt;> is a binary function. It takes two iterators. It would be possible to bind it all together using boost::bind, but it would be a pretty horrible mess. Something like:</p> <pre><code>boost::bind( &amp;std::reverse&lt;std::string::iterator&gt;, boost::bind(&amp;std::string::begin, _1), boost::bind(&amp;std::string::end, _1)) </code></pre> <p>Better, I think, would be to write a reusable function called reverse_range like so:</p> <pre><code>template &lt;class Range&gt; void reverse_range(Range&amp; range) { std::reverse(range.begin(), range.end()); } </code></pre> <p>(probably with some metaprogramming to ensure that Range&amp; isn't a double-reference)</p> <p>And then use that in your for_each (after adapting it to be a unary function, of course).</p> <pre><code>std::for_each(v.begin(), v.end(), std::ptr_fun(&amp;reverse_range&lt;std::string&gt;)); </code></pre> <p>EDIT:</p> <p>Because string::begin and string::end have both const and non-const variants, it is necessary to cast them (as litb discovered while I was off writing them to test my answer ... +1!). This makes it very verbose. Typedefs can make it a bit more sanitary, but to stick with the one-liner theme:</p> <pre><code>boost::bind( &amp;std::reverse&lt;std::string::iterator&gt;, boost::bind( (std::string::iterator (std::string::*)())&amp;std::string::begin, _1), boost::bind( (std::string::iterator (std::string::*)())&amp;std::string::end, _1) ) ); </code></pre> <p>Which just screams for refactoring.</p> <p>Finally, because I'm bored, bonus points for C++0x:</p> <pre><code>std::for_each(v.begin(), v.end() [](std::string&amp; s){ std::reverse(s); }); </code></pre> <p>EDIT: boost::bind works just fine, no need for boost::lambda.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload