Address: http://www.vimer.cn/2009/11/string%E6%9B%BF%E6%8D%A2%E6%89%80%E6%9C%89%E6%8C%87%E5% AE %9A%E5%AD%97%E7%AC%A6%E4%B8%B2%EF%BC%88c%EF%BC%89.html
# Include < String >
# Include < Iostream >
Using Namespace STD;
String Replace_all ( String STR, Const String Old_value, Const String New_value)
{
While ( True ){
String : Size_type pos ( 0 );
If (Pos = Str. Find (old_value )) ! = String : NPOs)
Str. Replace (Pos, old_value.length (), new_value );
Else Break ;
}
Return STR;
}
String Replace_all_distinct ( String STR, Const String Old_value, Const String New_value)
{
For ( String : Size_type pos ( 0 ); POS ! = String : NPOs; POS + = New_value.length ()){
If (Pos = Str. Find (old_value, POS )) ! = String : NPOs)
Str. Replace (Pos, old_value.length (), new_value );
Else Break ;
}
Return STR;
}
Int Main ()
{
Cout < Replace_all ( String ( " 12212 " ), " 12 " , " 21 " ) < Endl;
Cout < Replace_all_distinct ( String ( " 12212 " ), " 12 " , " 21 " ) < Endl;
End