String.replace(...)执行结果的不确定性?
String.replace(...)执行结果的不确定性?
---------------------------------------
本人在帮人写HDB3编码时,
遇到一个问题:
在String中使用replace时:
结果有时正确,有时错误.
请试一下下面的代码:
#include <iostream>
#include <string>
#include <stdio.h>
int main(){
string s("She am programming.");
cout<<s.size()<<endl;
s.replace(4,5,string("am00")); //结果有问题
cout<<s.size()<<endl;
cout<<s<<endl;
string s_("aa11223344bbee");
s_.replace(2,3,string("i am sleeping"));
cout<<s_<<endl; //结果是不正确的.
string str1("01234ppp56");
string str2("am0011111");
string str3;
str3 = str1;
str3.replace(1,4,string("am0011111"));
str1.replace(1,4,str2);
cout<<str1<<endl;
cout<<str3<<endl;
getchar();
return 1;
}