2017-04-22 56 views
1

標題不是很豐富,但基本上我試圖做的是對txt文件運行檢查,並找到包含我正在尋找的單詞。C++ else if字符串中的字符

下面的代碼正確地完成了它,並且完全如何我想要它來做到這一點。但!

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'; 
     } 
    } 
} 

我想以更整潔的方式做到這一點。

如果有人能指出我正確的方向,如cplusplus引用鏈接或其他文件將是很棒的。

+1

如果答案實際上已經回答了你的問題,那麼請將其標記爲已回答。不要將答案複製到你的問題中!這不是Stack Overflow的工作原理...... –

+0

如果代碼起作用,它更像是一個[Code Review](https://codereview.stackexchange.com/),它有自己的站點。 –

回答

0

以下的代碼以相同的順序執行作爲你的,並且是一點點整潔:

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

不要只是轉儲代碼;解釋問題並解釋解決方案。 –

0

數據替換的代碼。

創建一個容器,其中包含您正在查找的單詞,例如std::set<std::string>。然後添加一個內部循環,對每個單詞執行if檢查。

下面是一個例子:

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'; 
     } 
    } 
} 

std::set<std::string>不必具有自動存儲時間,如果它是永遠不變的。例如,您可以將其設置爲某個類的static數據成員。

無論如何,這個簡單的解決方案將是足夠的,除非你正在處理真正的大數據。在這種情況下,從算法的角度來看,您應該找到一種更精細的方式,但這取決於您的輸入如何實際構建。

另一個可能的擔心當且僅當這是性能關鍵代碼是一個std::set可能會有點慢。我選擇它是因爲它確保所有元素都是唯一的。但你總是可以用std::vector來代替它,看看你是否能夠獲得任何收益。

0

通過InternetAussie

回答您可以把所有的字符串到載體,並使用一個for循環迭代,並查找每個字符串:

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'; 
     } 
    } 
} 
}