简体   繁体   English

使用字符串查找和替换while循环内部时的std :: out_of_range

[英]std::out_of_range when using string find and replace inside while loop

So I have a task to convert all occurrences of some word in one string to another string. 因此,我有一个任务将某个字符串中某个单词的所有出现都转换为另一字符串。 But there is problem with condition of while loop which makes this error 但是while循环的条件存在问题,这会导致此错误

terminate called after throwing an instance of 'std::out_of_range' 抛出'std :: out_of_range'实例后调用终止

what(): basic_string::replace what():basic_string :: replace

This application has requested the Runtime to terminate it in an unusual way. 该应用程序已请求运行时以一种异常方式终止它。 Please contact the application's support team for more information. 请与应用程序的支持团队联系以获取更多信息。 Process returned 3 (0x3) execution time : 2.751 s 进程返回3(0x3)执行时间:2.751 s

My code is: 我的代码是:

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

Any suggestions? 有什么建议么?

You are checking if str.find(str2) had an occurrence comparing it to NULL , but this is wrong, because NULL is a macro that isn't meant for that and often expands into 0 , which can be a valid index. 您正在检查str.find(str2)是否将其与NULL进行比较,但这是错误的,因为NULL是一个str.find(str2)的宏,通常会扩展为0 ,这可以是一个有效的索引。 You should compare it to std::string::npos . 您应该将其与std::string::npos进行比较。 After doing this change, your code will work. 进行此更改后,您的代码即可使用。

Edit : std::string::npos corresponds to 18446744073709551615 when testing on coliru. 编辑: std::string::npos18446744073709551615测试时对应于18446744073709551615。 So that clearly isn't a valid index in your string. 因此,这显然不是字符串中的有效索引。

This condition 这种情况

while ( str.find(str2) != NULL ){

does not make sense because a call of find can return std::string::npos that is not equal to zero. 这是没有意义的,因为调用find可以返回不等于零的std::string::npos In this case the code has undefined behavior. 在这种情况下,代码具有未定义的行为。

You can apply the following approach 您可以采用以下方法

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

it's caused because str.find(str2) returns -1 if str2 is not existed in the str . 这是因为,如果str不存在str2str.find(str2)返回-1 You can use a variable pos to save the found position, so that you will not need to re-invoke find function. 您可以使用变量pos来保存找到的位置,这样就无需重新调用find函数。 The solution is supposed as following: 该解决方案假定如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM