C++ std::使用字符串查找和替换while循环时超出范围
因此,我有一项任务,将某个单词在一个字符串中的所有出现转换为另一个字符串。但while循环的条件存在问题,导致了这种错误 在抛出'std::out_of_range'的实例后调用terminate what():基本字符串::替换 此应用程序已请求运行时以异常方式终止它。有关更多信息,请联系应用程序的支持团队。进程返回3(0x3)执行时间:2.751秒 我的代码是:C++ std::使用字符串查找和替换while循环时超出范围,c++,string,replace,while-loop,find,C++,String,Replace,While Loop,Find,因此,我有一项任务,将某个单词在一个字符串中的所有出现转换为另一个字符串。但while循环的条件存在问题,导致了这种错误 在抛出'std::out_of_range'的实例后调用terminate what():基本字符串::替换 此应用程序已请求运行时以异常方式终止它。有关更多信息,请联系应用程序的支持团队。进程返回3(0x3)执行时间:2.751秒 我的代码是: #include <iostream> #include <string> using namespace
#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;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串str2(“三”);
字符串str(“一三二四三三”);
while(str.find(str2)!=NULL){
str.replace(str.find(str2),str2.length(),“五”);
cout您正在检查str.find(str2)
曾出现过将其与NULL
进行比较的情况,但这是错误的,因为NULL
是一个不适用于此的宏,通常会扩展为0
,这可能是一个有效的索引。您应该将其与std::string::npos
进行比较。进行此更改后,代码将正常工作
编辑:
std::string::npos
在coliru上测试时对应于18446744073709551615
。因此,这显然不是字符串中的有效索引。您正在检查str.find(str2)曾出现过将其与NULL
进行比较的情况,但这是错误的,因为NULL
是一个不适用于此的宏,通常会扩展为0
,这可能是一个有效的索引。您应该将其与std::string::npos
进行比较。进行此更改后,代码将正常工作
编辑:std::string::npos
在coliru上测试时对应于18446744073709551615
。因此,这显然不是字符串中的有效索引。此条件
while ( str.find(str2) != NULL ){
没有意义,因为调用find
可以返回不等于零的std::string::npos
。在这种情况下,代码具有未定义的行为
您可以应用以下方法
std::string str2("three");
std::string str("one three two four three three");
const char *five = "five";
size_t n = std::strlen(five);
for (std::string::size_type pos = 0;
( pos = str.find(str2, pos) ) != std::string::npos; pos += n)
{
str.replace(pos, str2.length(), five);
}
这种情况
while ( str.find(str2) != NULL ){
没有意义,因为调用find
可以返回不等于零的std::string::npos
。在这种情况下,代码具有未定义的行为
您可以应用以下方法
std::string str2("three");
std::string str("one three two four three three");
const char *five = "five";
size_t n = std::strlen(five);
for (std::string::size_type pos = 0;
( pos = str.find(str2, pos) ) != std::string::npos; pos += n)
{
str.replace(pos, str2.length(), five);
}
这是因为
str.find(str2)
返回-1
如果str
中不存在str2
。您可以使用变量pos
保存找到的位置,这样您就不需要重新调用find
函数。解决方案如下:
#include <iostream>
#include <string>
using namespace std;
int main () {
string str2("three");
string str("one three two four three three");
int pos = str.find(str2);
while (pos > 0) {
str.replace(pos, str2.length(), "five");
pos = str.find(str2);
cout << str << endl; // i put it inside loop to see output
}
cout << str << endl;
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串str2(“三”);
字符串str(“一三二四三三”);
int pos=str.find(str2);
而(位置>0){
str.replace(位置,str2.length(),“五”);
pos=str.find(str2);
cout这是因为str.find(str2)
返回-1
如果str
中不存在str2
。您可以使用变量pos
保存找到的位置,这样您就不需要重新调用find
函数。解决方案如下:
#include <iostream>
#include <string>
using namespace std;
int main () {
string str2("three");
string str("one three two four three three");
int pos = str.find(str2);
while (pos > 0) {
str.replace(pos, str2.length(), "five");
pos = str.find(str2);
cout << str << endl; // i put it inside loop to see output
}
cout << str << endl;
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串str2(“三”);
字符串str(“一三二四三三”);
int pos=str.find(str2);
而(位置>0){
str.replace(位置,str2.length(),“五”);
pos=str.find(str2);
谢谢你的帮助。