简体   繁体   English

在为我的程序抛出一个 'std::out_of_range' 实例后调用 Terminate 来查找和替换字符串中的单词

[英]Terminate called after throwing an instance of 'std::out_of_range' for my program which finds and replaces words in a string

beginner here, Im getting this error within my program which is supposed to find a word in a string then, replace that word with any that you input.初学者,我在我的程序中遇到这个错误,它应该在字符串中找到一个单词,然后用您输入的任何单词替换该单词。 When I input multiple words into the str1, it says this:当我在 str1 中输入多个单词时,它说:

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase:__pos (which is 18446744074709551615) > this->size() (which is 9)

Heres my code:这是我的代码:

#include <iostream>
#include <string>

using namespace std;

void findReplace(string& str1,string& str2,string& str3);

int main() 
{
string str1, str2, str3;

cout << "Enter a sentence that you would like to analyze: \n";
cin >> str1;
cin.ignore();
cout << "Enter a word that you would like to search for: \n";
cin >> str2;
cin.ignore();
cout << "Enter a word that would replace the word that was found: \n";
cin >> str3;
cin.ignore();

findReplace(str1,str2,str3);

return 0;
}

void findReplace(string& str1,string& str2,string& str3)
{
  int length= 0;
  int str2len= 0;
  int str3Length = 0;

  length = str1.length();
  str2len = str2.length();
  str3Length = str3.length();
  
  int found = str1.find(str2);

  if ((found!= string::npos))
  {
    cout << str2 << " found at " << found << endl;
  }

  str1.erase(found, str2len);

  str1.replace(found, str3Length, str3 );
  
  cout << str1;

}

The error message contains two big hints: basic_string::erase , the exception was thrown in the call to erase and 18446744074709551615 is maximum 64 bit unsigned in int which on a modern 64 bit system matches the definition of npos错误消息包含两个重要提示: basic_string::erase ,在调用erase时抛出异常, 18446744074709551615是最大 64 位 unsigned in int ,在现代 64 位系统上与npos的定义匹配

static const size_type npos = -1;

So let's take a look at what lead up to the call to erase :那么让我们来看看是什么导致了调用erase

  int found = str1.find(str2); // find str2

  if ((found!= string::npos))
  { // found str2
    cout << str2 << " found at " << found << endl; // print that we found it
  }

  str1.erase(found, str2len); // erase it (even if we didn't find it)

  str1.replace(found, str3Length, str3 ); // replace it (even if we didn't find it)

Fix:使固定:

  auto found = str1.find(str2); // Note: Position ISN'T an int. auto will 
                                // find and use correct type. If auto's not available
                                // size_t will do the job.

  if ((found!= string::npos))
  { // found str2
    cout << str2 << " found at " << found << endl; // print that we found it
    str1.erase(found, str2len); // erase it 

    str1.replace(found, str3Length, str3 ); // replace it 
  }

In addition:此外:

cin >> str1;

will read only one word.只会读一个字。 Since you need a sentence, use std::getline既然需要一句话,就 std::getline

You can discard all of the cin.ignore() s.您可以丢弃所有cin.ignore() s。 They are useless here.他们在这里没用。 >> will consume any leftover whitespace before the token tit finds. >>将在标记山雀找到之前消耗任何剩余的空白。 It will leave whitespace after the token, so watch out for Why does std::getline() skip input after a formatted extraction?它会在标记后留下空格,所以请注意为什么在格式化提取后 std::getline() 会跳过输入? if you place a std::getline after a >> .如果在>>之后放置std::getline

暂无
暂无

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

相关问题 在抛出“std::out_of_range”的实例后调用终止? - terminate called after throwing an instance of 'std::out_of_range'? 抛出&#39;std :: out_of_range&#39;实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' 抛出&#39;std :: out_of_range&#39;实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' 抛出&#39;std :: out_of_range&#39;实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' 抛出&#39;std :: out_of_range&#39;的实例之后调用终止终止what():basic_string :: substr:__pos(为1)&gt; this-&gt; size()(为0) - terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 1) > this->size() (which is 0) 在抛出 &#39;std::out_of_range&#39; what(): array::at: __n (即 1990878449) &gt;= _Nm (即 5) 的实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' what(): array::at: __n (which is 1990878449) >= _Nm (which is 5) 将字母向前移动3个字母的程序,错误:抛出&#39;std :: out_of_range&#39;实例后终止终止 - Program that shifts letters 3 letters forward, error: terminate called after throwing an instance of 'std::out_of_range' 程序以信号 SIGABRT 终止,已中止。 在抛出 'std::out_of_range' 的实例后调用终止 - Program terminated with signal SIGABRT, Aborted. terminate called after throwing an instance of 'std::out_of_range' 抛出&#39;std :: out_of_range&#39;what():basic_string :: insert实例后,c ++终止调用 - c++ terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::insert c++ 在抛出&#39;std::out_of_range&#39; what() 实例后调用终止:basic_string::substr: - c++ terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM