求C++替换字符代码
我写的参考
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;
}
使用时如
replace_all_distinct(str,"0","a");
可以将str中的所有“0"替换为"a"
热门标签: