#include #include #include #include #include #include using namespace std; inline string replaceAll( const string::const_iterator& start, const string::const_iterator& end, const string& f, const string& r ) { if ( f.empty() ) { return string( start, end ); } ostringstream build_it; typedef string::const_iterator iter; iter i( start ); const iter::difference_type f_size( distance( f.begin(), f.end() ) ); for ( iter pos; ( pos = search( i , end, f.begin(), f.end() ) ) != end; ) { copy( i, pos, ostreambuf_iterator( build_it ) ); copy( r.begin(), r.end(), ostreambuf_iterator( build_it ) ); advance( pos, f_size); i = pos; } copy( i, end, ostreambuf_iterator( build_it ) ); return build_it.str(); } int main( ) { const string s( "(0 1 2 3 4 5 6 7 8 9)" ); cout << replaceAll( s.begin() + 1 , s.end() - 1 , " ", "," ) << endl; } /* g++ -Wall -Wextra this.cpp -o this -O3 -msse3 -mtune=i686 -s */