There's also replace_if:
Code:
#include <iostream>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;
int main()
{
string str = "This_is_a_string";
cout << str << endl;
replace_if(str.begin(),str.end(),bind1st(equal_to<char>(),'_'),' ');
cout << str << endl;
return 0;
}
Output:
Code:
This_is_a_string
This is a string
[edit]
You can even use for_each:
Code:
#include <iostream>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;
template<char char_to_find,char char_to_replace>
class replaceit
{
public:
operator()(char& c)
{
if( c == char_to_find ) c = char_to_replace;
}
};
int main()
{
string str = "This_is_a_string";
cout << str << endl;
for_each(str.begin(),str.end(),replaceit<'_',' '>());
cout << str << endl;
return 0;
}
Should output the same thing.
[/edit]