【发布时间】:2017-05-13 22:45:05
【问题描述】:
所以我的任务是将一个字符串中某个单词的所有出现转换为另一个字符串。但是while循环的条件有问题导致这个错误
在抛出 'std::out_of_range' 的实例后调用终止
what(): basic_string::replace
此应用程序已请求运行时以不寻常的方式终止它。请联系应用程序的支持团队以获取更多信息。进程返回 3 (0x3) 执行时间:2.751 s
我的代码是:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str2("three");
string str("one three two four three three");
while ( str.find(str2) != NULL ){
str.replace(str.find(str2),str2.length(),"five");
cout << str << endl; // i put it inside loop to see output
}
cout << str << endl;
return 0;
}
有什么建议吗?
【问题讨论】:
标签: c++ string replace while-loop find