C++ 当目标字写入两次时,程序无法继续

C++ 当目标字写入两次时,程序无法继续,c++,istringstream,C++,Istringstream,我的源代码: #include <cstdlib> #include <iostream> #include <fstream> #include <cstring> #include <sstream> #include <string> #include <algorithm> using namespace std; int main(int argc, char *argv[]){ strin

我的源代码:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>

using namespace std;

int main(int argc, char *argv[]){
    string target_word=argv[1];     //The word you want to change
    string changed_word=argv[2];     ///The word you want to change it to
    //target_word=target_word+' ';
    string line;     //Used hold each line during processes
    string word;     //Used to hold each word in the line during processes
    size_t position;     //Used to hold the position of the first character in the word

    while(getline(cin, line)){     //Grab each line one at a time
        istringstream iss(line);
        while(iss>>word){     //Grab each word from the line one at a time
            if(word==target_word){     //Check if the current word is the target word
                position=line.find(target_word);     //Find the starting position of the word
                line.replace(position, word.length(), changed_word);     //Change the target word to the word you chose to change it to
            }
        }
        cout<<line<<"\n";     //Output the altered line
    }
    //cout<<target_word<<"END"<<'\n';
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
字符串target_word=argv[1];//要更改的单词
字符串已更改_word=argv[2];///要将其更改为的单词
//目标词=目标词+“”;
string line;//用于在进程中保留每一行
string word;//用于在处理过程中保留行中的每个单词
size\u t position;//用于保持单词中第一个字符的位置
while(getline(cin,line)){//一次抓取一行
istringstream iss(线);
while(iss>>word){//从一行中一个一个地抓取每个单词
如果(word==target\u word){//检查当前单词是否为目标单词
position=line.find(target_word);//查找单词的起始位置
line.replace(position,word.length(),changed_word);//将目标单词更改为您选择的单词
}
}
库特
将获取一个以空格分隔的单词并将其存储在
word
中。如果输入为“input-input”,则流将“input”写入
word
并在空格处停止。在下一次迭代中,将读取下一个“input”。但如果输入为“inputinput”,则没有空格来分隔第一个“input”,因此“inputinput”读入
word
。这与“输入”不匹配,它太长了,因此不会发生替换

编辑:我想我看到了混乱的一部分。让我们检查一下“海象鱼缸”的输出,其中“鱼”是要替换的词,“鸟”是替换词

Iteration 1: walrus != fish. No replacement.   Line = "walrus fishfish fish tank" 
Iteration 2: fishfish != fish. No replacement. Line = "walrus fishfish fish tank" 
Iteration 3: fish == fish. Replace first fish. Line = "walrus birdfish fish tank" 
Iteration 4: tank != fish. No replacement.     Line = "walrus birdfish fish tank" 
第三条“鱼”最终取代了第一条“鱼”,因为它是唯一被认可的鱼。这给了我们一个提示,我们应该使用什么来代替
stringstream
>

编辑结束

与其使用
stringstream
>
来解析行,您最好还是坚持使用
find

auto pos = line.find(target_word);
while (pos != string::npos)
{
    line.replace(pos, target_word.length(), changed_word);
    pos = line.find(target_word);
}
或者更简单但不太明显:

while ((auto pos = line.find(target_word)) != string::npos)
{
    line.replace(pos, target_word.length(), changed_word);
}
请注意,如果
target\u word
在线并且
target\u word==changed\u word
,这将导致一个无限循环,因为它将始终查找
target\u word
,并用
target\u word
替换
target\u word。这种情况可以提前检查,并且程序可以输入一个不同的路径,只打印用户的输入b确认它们。用户将无法区分它们之间的差异

while ((auto pos = line.find(target_word)) != string::npos)
{
    line.replace(pos, target_word.length(), changed_word);
}