2016-03-24 8 views
-1
#include <iostream> 
#include <string> 
#include <sstream> 
#include <fstream> 
using namespace std; 




       infile.get(array[position]); 
       position++; 
      } 
      array[position - 1] = '\0'; 
      for (int i = 0; array[i] != '\0'; i++) 
      { 
       cout << array[i]; 
      } 

     } 
    } 
    cout << "Displaying Array..." << array << endl; 

    //stringstream buffer; 
    //buffer << in.rdbuf(); 
    //std::string test = buffer.str(); 
    //std::cout << test << std::endl << std::endl; 
    // 

    system("pause"); 
} 

私は、テキストファイルを検索するプログラムを作成しようとしていますが、1980年代にマンチェスター大学で学士号を取得しました。私の「並列コンピューティング」クラスでは、世界を占める並列コンピューティングアーキテクチャであるマンチェスターデータフローマシンについて講義を行いました。C++索引を検索する

私は2000年代にワシントン大学で修士号を取得しました。私の「並列コンピューティング」クラスでは、マンチェスターデータフローマシンについての講義がありましたが、なぜそれが世界を占めていないのですか?

20年後、私たちは並列コンピューティングについて多くのアイデアを思いついたようですすべてを結びつけるという単一のアイデアを打ち負かすことはありません。シリアルコンピューティングでは、フォンノイマンアーキテクチャを採用しています。キャッシュや分岐予測、パイプライニングなどがありますが、基本マシンはフォンノイマンマシンと見なすことができます。

私が見てきた最も実用的なアプローチは、並列プログラミングパターンの使用です。あなたが誰と話をしているのか、どのようにパターンをグループ化するかによって、これらのパターンは13〜20の間にあります。私は、マイケル・マックール博士のパターンの説明には部分的です。私はテキスト・ファイルをリンクさせることができましたが、次のステップはファイルを検索するためのコードを作成することではありません。どんな助けも素晴らしいだろう。

おそらくアレイを作成し、各要素を通過させることは考えられますが、わからないと考えました。どんな助けでも本当に役に立ちます。 配列内の位置。

はありがとう

+2

適切)(stream.eof使用してください - それを避ける - インターネット –

+0

を検索(infile.eofは()) '間違っている間、この'、それが起動することはありませんファイルに何かが含まれている場合 – DimChtz

+0

@DimChtzあなたはいくつか、それを修正する方法を教えてください –

答えて

1

例えばそこクヌースモリス・プラット、またはラビン - カープのようなアルゴリズムの多くは、あります。私はウィキペディアがそれらを完全に記述していると思う。しかし、彼らは通常1つの文字列に適しています。複数の文字列の場合、最良のアルゴリズムは、テキストからサフィックスツリーを構築し、ツリーを検索することです。

0

最も簡単な方法は、標準ライブラリを使用することです。現在のところ、検索は線形ですが、パフォーマンスが鍵であれば、ライブラリは簡単な並列化を可能にします。

ここでは標準ライブラリ(C++ 11が必要)のみを使用するソリューションがあります。

ストリームから文字列を読み取る正しい方法に注意してください。

#include <iostream> 
#include <string> 
#include <vector> 
#include <map> 
#include <sstream> 
#include <algorithm> 
#include <iomanip> 

using search_term_vector = std::vector<std::string>; 
using search_results = std::map<std::string, std::vector<std::size_t>>; 

std::vector<std::string> load_search_terms() 
{ 
    constexpr char search_term_file[] = "pig dog cat garbage"; 
    std::istringstream is(search_term_file); 

    std::vector<std::string> search_terms; 
    std::string term_buffer; 
    while (is >> term_buffer) { 
     search_terms.push_back(std::move(term_buffer)); 
    } 
    return search_terms; 
} 

search_results search_space_for_terms(const std::string& search_space, const search_term_vector& terms) 
{ 
    search_results results; 

    for (auto const& term : terms) 
    { 
     auto& result = results[term]; 
     auto icurrent = std::begin(search_space); 
     while (icurrent != std::end(search_space)) 
     { 
      auto ipos = std::search(icurrent, std::end(search_space), 
            std::begin(term), std::end(term)); 
      if (ipos != std::end(search_space)) 
      { 
       result.push_back(std::distance(std::begin(search_space), ipos)); 
       std::advance(ipos, term.length()); 
      } 
      icurrent = ipos; 
     } 
    } 

    return results; 
} 

int main() 
{ 
    using namespace std::string_literals; 

    auto search_space = "tdogicatzhpigu"s; 
    auto search_terms = load_search_terms(); 
    auto results = search_space_for_terms(search_space, search_terms); 

    for (auto const& result_pair : results) 
    { 
     std::cout << std::quoted(result_pair.first) << " was found "; 
     auto const& locations = result_pair.second; 
     if (locations.empty()) 
     { 
      std::cout << "nowhere" << std::endl; 
     } 
     else 
     { 
      auto sep = (locations.size() > 1) ? "at positions " : "at position "; 
      for (auto pos : locations) 
      { 
       std::cout << sep << pos; 
       sep = ", "; 
      } 
      std::cout << std::endl; 
     } 
    } 

    return 0; 
} 

期待される結果:

"cat" was found at position 5 
"dog" was found at position 1 
"garbage" was found nowhere 
"pig" was found at position 10 
関連する問題