Reversing strings in a vector using for_each and bind
        Posted  
        
            by fmuecke
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by fmuecke
        
        
        
        Published on 2009-09-30T09:38:43Z
        Indexed on 
            2010/05/15
            21:04 UTC
        
        
        Read the original article
        Hit count: 211
        
Hi! I was wandering how it's possible to reverese strings that are contained in a vector using a single for_each command just in one "simple" line.
Yea, I know it is easy with a custom functor, but I can't accept, that it can't be done using bind (at least I couldn't do it).
#include <vector>
#include <string>
#include <algorithm>
std::vector<std::string> v; 
v.push_back("abc");
v.push_back("12345");
std::for_each(v.begin(), v.end(), /*call std::reverse for each element*/);
Edit: Thanks a lot for those funtastic solutions. However, the solution for me was not to use the tr1::bind that comes with the Visual Studio 2008 feature pack/SP1. I don't know why it does not work like expected but that's the way it is (even MS admits that it's buggy). Maybe some hotfixes will help.
With boost::bind everything works like desired and is so easy (but sometimes relly messy:)). I really should have tried boost::bind in the first place...
© Stack Overflow or respective owner