[英]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)
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.