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, '~'), ' ');

About these ads

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

  1. 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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s