2017-04-22 3 views
1

Titel ist nicht sehr informativ, aber im Grunde, was ich versuche zu tun, ist eine Prüfung gegen eine TXT-Datei und finde Wörter, die das, was ich suche.C++ sonst, wenn char in string

Der folgende Code tut es richtig und genau, wie ich es tun will. ABER!

void qu() 
{ 
    for (Word word : word2) 
    { 
     string uq = word.getWord(); 
     if (uq.find("qa") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qb")!= std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qc") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qd") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qe") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qf") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qg") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qh") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qi") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qj") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qk") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("ql") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qm") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qn") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qo") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qp") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qq") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qr") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qs") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qt") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qv") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qw") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qx") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qy") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
     else if (uq.find("qz") != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
    } 
} 

Ich möchte es auf eine sauberere Art und Weise tun.

Wenn mir jemand in die richtige Richtung zeigen könnte wie ein cplusplus Verweis Verweis oder eine andere Dokumentation, die toll wäre.

+1

Wenn eine Antwort Ihre Frage wirklich beantwortet hat, dann bitte markieren Sie es als beantwortet. Kopiere die Antwort nicht in deine Frage! So funktioniert Stack Overflow nicht ... –

+0

Wenn der Code funktioniert, ist es eher ein Fall von [Code Review] (https://codereview.stackexchange.com/), der eine eigene Site hat. –

Antwort

0

Der folgende Code wird in der gleichen Reihenfolge wie bei Ihnen durchführen, und ist ein TAD ordentlicheres:

for (Word word : word2) 
{ 
    string uq = word.getWord(); 
    string lookup = "qa"; 
    for (char c: "abcdefghijklmnopqrstuvwxyz") 
    { 
     lookup[1] = c; 
     if (uq.find(lookup) != std::string::npos) 
     { 
      cout << uq << '\n'; 
      break; 
     } 
    } 
} 
+0

Dump nicht nur Code; erkläre das Problem und erkläre die Lösung. –

0

Ersetzen Code mit Daten.

Erstellen Sie einen Container, der die Wörter enthält, nach denen Sie suchen, z. B. std::set<std::string>. Fügen Sie dann eine innere Schleife hinzu, die die if Überprüfung für jedes Wort durchführt. Hier

ein Beispiel:

std::set<std::string> const search_words = { "qa", "qb", "qc", /*...*/ "qz" }; 
for (auto const& word : word2) 
{ 
    std::string const uq = word.getWord(); 
    for (auto const& search_word : search_words) 
    { 
     if (uq.find(search_word) != std::string::npos) 
     { 
      std::cout << uq << '\n'; 
     } 
    } 
} 

Die std::set<std::string> braucht keine automatische Speicherdauer zu haben, wenn es immer das gleiche ist. Sie könnten es zum Beispiel zu einem static Datenelement einer Klasse machen.

In jedem Fall wird diese einfache Lösung ausreichen, es sei denn, Sie haben mit wirklich großen Daten zu tun. In diesem Fall sollten Sie einen ausgefeilteren Weg aus einer algorithmischen Perspektive finden, aber das hängt davon ab, wie Ihre Eingabe tatsächlich strukturiert ist.

Eine andere mögliche Sorge wenn und nur wenn das ist Performance-kritischen Code ist, dass eine std::set kann etwas langsam sein. Ich habe es gewählt, weil es garantiert, dass alle Elemente einzigartig sind. Aber Sie können es immer durch eine std::vector ersetzen und sehen, ob das Ihnen irgendetwas bringt.

0

Beantwortet von InternetAussie

Sie können alle Saiten in einen Vektor setzen und eine for-Schleife für jeden String iterieren und aussehen:

void qu() 
{ 
for (Word word : word2) 
{ 
    std::string uq = word.getWord(); 
    std::vector<std::string> strings_to_find = 
    { 
     "qa", "qb", "qc", "qd", "qe", "qf", "qg", "qh", "qi", "qj", "qk", 
     "ql", "qm", "qn", "qo", "qp", "qr", "qs", "qt", "qv", "qw", "qx", 
     "qy", "qz" 
    }; // no "qu" in original code 

    for (auto& str : strings_to_find) 
    { 
     if (uq.find(str) != std::string::npos) 
     { 
      cout << uq << '\n'; 
     } 
    } 
} 
}