std::replace_if & boost::bind

May 7, 2008

I wanted to use std::replace_if( ) to substitute spaces for the tilde symbol (~) in a string. The STL docs give an example that uses std::bind2nd. MS has a similar example in their knowledge base. This is what I came up with to do the job using boost::bind

std::string rep("one~two");
std::replace_if( rep.begin( ), rep.end( ), boost::bind( std::equal_to<char>( ), _1, '~'), ' ');

One Response to “std::replace_if & boost::bind”

  1. Derek's avatar Derek Says:

    Why do not you use std::replace directly as below:
    std::string rep(“one~two”);
    std::replace( rep.begin(),rep.end(), ‘~’, ‘ ‘);

Leave a comment