gpt4 book ai didi

C++ s.replace 不会用 "."替换 ".\n"

转载 作者:行者123 更新时间:2023-11-30 01:38:32 25 4
gpt4 key购买 nike

到目前为止,我已经添加了两个查找和替换,并且一切正常。但是当我添加第三个查找和替换时,在找到一个点的地方添加一个\n,整个控制台输出变为空白。所有文字消失。知道我做错了什么吗?

这是我要添加的代码,用于替换“.”带有“.\n”,这会导致打印变成空白。

while (t1.find(".") != string::npos)
t1.replace(t1.find("."), 3, ".\n");

在我添加第三个查找和替换之前,这是剩下的所有代码:

#include <iostream>
#include <string>
using namespace std;

int main()
{
setlocale(LC_ALL, "swedish");
string const txt1 = "Foten är en kroppsdel som förekommer mycket i våra uttryck.";
string const txt2 = "På stående fot betyder omedelbart, utan förberedelse.";
string const txt3 = "Försätta på fri fot betyder att ge någon friheten.";
string const txt4 = "Sätta foten i munnen betyder att göra bort sig.";
string const txt5 = "Få om bakfoten betyder att missuppfatta något.";
string const txt6 = "Skrapa med foten betyder att visa sig underdånig eller ödmjuk.";
string const txt7 = "Stryka på foten betyder att tvingas ge upp.";
string const txt8 = "Leva på stor fot betyder att föra ett dyrbart eller slösaktigt leverne.";
string const txt9 = "Varför fick du foten???";

string t1 = txt1 + txt2 + txt3 + txt4 + txt5 + txt4 + txt5 + txt6 + txt7 + txt8 + txt9;

while (t1.find("fot") != string::npos)
t1.replace(t1.find("fot"), 3, "hand");

while (t1.find("Fot") != string::npos)
t1.replace(t1.find("Fot"), 3, "Hand");

cout << t1;


return 0;

}

最佳答案

替换字符串后不要从前面搜索字符串!

#include <iostream>
#include <string>

void string_replace_all(std::string& target, std::string_view search_str, std::string_view replace_str)
{
for(
//find first search_str in target
std::size_t pos = target.find(search_str);
//When pos == std::string::npos, not match was found.
pos != std::string::npos;
// Pos is front of last replaced string.
// pos + replace_str.size() is just after replaced string.
// We need to search string from there to avoid infinity loop.
pos = target.find(search_str, pos + replace_str.size())
){
target.replace(pos, search_str.size(), replace_str);
}
}
int main()
{
setlocale(LC_ALL, "swedish");
std::string const txt1 = "Foten är en kroppsdel som förekommer mycket i våra uttryck.";
std::string const txt2 = "På stående fot betyder omedelbart, utan förberedelse.";
std::string const txt3 = "Försätta på fri fot betyder att ge någon friheten.";
std::string const txt4 = "Sätta foten i munnen betyder att göra bort sig.";
std::string const txt5 = "Få om bakfoten betyder att missuppfatta något.";
std::string const txt6 = "Skrapa med foten betyder att visa sig underdånig eller ödmjuk.";
std::string const txt7 = "Stryka på foten betyder att tvingas ge upp.";
std::string const txt8 = "Leva på stor fot betyder att föra ett dyrbart eller slösaktigt leverne.";
std::string const txt9 = "Varför fick du foten???";

std::string t1 = txt1 + txt2 + txt3 + txt4 + txt5 + txt4 + txt5 + txt6 + txt7 + txt8 + txt9;

string_replace_all(t1, "fot", "hand");
string_replace_all(t1, "Fot", "Hand");
string_replace_all(t1, ".", ".\n");


std::cout << t1;


return 0;

}

https://wandbox.org/permlink/KejSz05Ja0bu9mN8

关于C++ s.replace 不会用 "."替换 ".\n",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47618792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com