2017-04-22 3 views
1

タイトルはあまり有益ではありませんが、基本的にはtxtファイルに対してチェックを行い、探しているものが含まれている単語を探します。C++ else文字列内の文字の場合

次のコードは、それを正しく正確に実行します。しかし!

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のrefernceのリンクや他の素晴らしいドキュメントがあります。

+1

答えは実際にあなたの質問に答えている場合は、それを回答としてマークしてください。あなたの質問に答えをコピーしないでください!それはスタックオーバーフローの仕組みではありません... –

+0

コードが動作する場合は、それ自身のサイトを持つ[コードレビュー](https://codereview.stackexchange.com/)のケースの多くです。 –

答えて

0

以下のコードは、あなたと同じ順序で実行し、かつすっきりTADであろう。

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に置き換えて、それがあなたに何かを与えるかどうか確認することができます。 InternetAussie

によって回答

0

あなたは、ベクター中にすべての文字列を入れて、反復処理し、各文字列を探すために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'; 
     } 
    } 
} 
} 
関連する問題