-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");
}

i am trying to make a program, that will search through the text file, for certain woI did my undergraduate degree at Manchester University in the 1980s. In my “Parallel Computing” class we had a lecture about the Manchester Dataflow Machine – a parallel computing architecture that was going to take over the world.

I did my master’s degree at the University of Washington in the 2000s. In my “Parallel Computing” class we had a lecture about the Manchester Dataflow Machine, and why it didn’t take over the world.*

In 20 years it seems like we came up with a lot of ideas for parallel computing, without ever hitting on the single idea that ties everything together. For serial computing we have the Von Neumann architecture, and even though we now have caches and branch prediction and pipelining and more, we can still regard the basic machine as a Von Neumann machine.

The most practical approach I have seen is the use of parallel programming patterns. Depending on who you talk to, and how they group the patterns there are somewhere between 13 and 20 of these. I am partial to Dr. Michael McCool’s explanation of the patterns:ent i have managed to get the text file to be linked, but i am not sure what the next step would be to create code to search through the files, for certain words. Any help would be great.

I have thought about possibly maybe creating an array, then making it go through each element but not sure. Any help would be really helpful. e location within the array.

Thank you

kas tozu
  • 11
  • 5
  • 2
    Please use stream.eof() properly - avoid it - search the internet –  Mar 24 '16 at 21:12
  • This `while (infile.eof())` is wrong, it will never start, if the file contains something. – DimChtz Mar 24 '16 at 21:13
  • @DimChtz could you please give some, how to fix it – kas tozu Mar 24 '16 at 21:17
  • @kastozu Why so many `infile.eof()` in your code? For start change first `while (infile.eof())` to `while (!infile.eof())` – DimChtz Mar 24 '16 at 21:19
  • @DimChtz Prefer not using any sort of while eof. It never freaking works. Seriously. Just don't do it. This logic reads like, "Is my data good? OK now read it." Think on that. How can you know if the data is any good before you read it? Read, then test. Next, Testing eof is not sufficient. Other things could go wrong, results in infinite loops because the stream is now unreadable, but not the end of the file and will never reach the end of file because the stream is unreadable. More here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Mar 24 '16 at 21:58
  • @user4581301 Totally agree with you. My intention was: first to help him make his code working and then tell him to use `while(infile >> data)` instead of `eof` :) – DimChtz Mar 24 '16 at 22:03

2 Answers2

1

There are plenty of algorithms out there like Knuth Morris Pratt, or Rabin-Karp, for example. I think wikipedia describes them perfectly. But they're usually good for 1 string. For multiple strings the best algorithm is to build a suffix tree from the text and search the tree.

Pavel
  • 1
  • 2
  • 14
  • 43
0

The easiest way is to use the standard library. Currently the searches are linear, but as of c++17 the library will allow easy parallelisation if performance is key.

Here's a solution that uses only the standard library (c++11 required).

Note the correct way to read strings from a stream.

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

expected results:

"cat" was found at position 5
"dog" was found at position 1
"garbage" was found nowhere
"pig" was found at position 10
Richard Hodges
  • 64,204
  • 6
  • 75
  • 124