2017-12-03 3 views
1

나는 두 개의 찾기와 바꾸기를 추가했으며, 모두 잘 작동합니다. 그러나 세 번째 찾기 및 바꾸기를 추가하면 \ n을 추가하여 점이있는 곳을 찾으면 전체 콘솔 출력이 비어있게됩니다. 모든 텍스트가 사라집니다. 내가 뭘 잘못하고 있는거야?C++ s.replace는 "."대체하지 않습니다. "와 함께. n"

이것은 "."을 (를) 대체하기 위해 추가하려는 코드입니다. ". \ 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; 

} 
+0

내가 C++ 모르지만, 어쩌면 정규 표현식을 사용하고 있습니다; 그리고 RegExp에서'.'는 "어떤 종류의 한 문자"를 의미합니다. 따라서 모든 텍스트를 "\ n"으로 변경해야합니다. – NatNgs

+0

나는 네가 옳다는 것을 실제로 믿는다. 그것이 점이라고 어떻게 이해해야합니까? – Thesar

+0

어쩌면 점을 "\"와 같이 이스케이프 해 봅니다. – NatNgs

답변

2

문자열을 바꾸면 앞에서 문자열을 검색하지 마십시오!

#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

+0

std :: string_view가 작동하지 않습니다. std doesnt 멤버 'string_'view'가 있다고합니까? – Thesar

+0

std :: string_view는 C++ 17의 새 라이브러리입니다. C++ 환경이 없다면,'std :: string_view'를'std :: string' (할당이 발생 함)으로 대체하십시오. 아니면, 부스트를 사용할 수 있다면,'boost :: string_view'로 대체하십시오. – yumetodo

+0

고맙습니다. 혹시라도 코드 시작 부분에있는이 void replace_all에 대해 읽을 수있는 곳을 참조 할 수 있습니까? 그래서 나는 그것이 무엇을하는지 이해합니다. 그러므로 나중에 추모하기가 더 쉽습니다. – Thesar

2

을 : 나는 세 번째 찾기를 추가하고 교체하기 전에

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

모든 godd 작동 코드의 나머지 부분입니다. t1.find(".") != string::npos".\n" 대체 문자열의 일부로 '.'을 계속 삽입하기 때문에 계속해서 true으로 유지됩니다.

용액은 replace 삽입했다 내용 후 루프마다 새로운 find 검색을 시작하는 것이다.


어쨌든 Boost String Algorithms을 사용하실 수 있습니다. 그런 다음 boost::replace_all(t1, ".", ".\n");의 간단한 문제가됩니다.

+0

와우, 어떻게 보지 못했죠? 그래서 나 바보. 나는 또한 모든 뒤에 \ n을 삽입하려고 시도했으나 어느 것도 작동하지 않습니다. 솔루션 정보가 있습니까? – Thesar

관련 문제