Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::使用字符串查找和替换while循环时超出范围_C++_String_Replace_While Loop_Find - Fatal编程技术网

C++ std::使用字符串查找和替换while循环时超出范围

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

因此,我有一项任务,将某个单词在一个字符串中的所有出现转换为另一个字符串。但while循环的条件存在问题,导致了这种错误

在抛出'std::out_of_range'的实例后调用terminate

what():基本字符串::替换

此应用程序已请求运行时以异常方式终止它。有关更多信息,请联系应用程序的支持团队。进程返回3(0x3)执行时间:2.751秒

我的代码是:

#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);

谢谢你的帮助。