Parallel scan of sorted sequences

I need to take an unknown amount of sorted files and then output any number that is in at least half of them... I know I need to read in the files to a vector and then iterate through them all at the same time looking at the lowest number first and so on. But I am stuck at the point of taking an unknown amount of files and putting them in a container. PLEASE HELP!


This is what I have so far but the vector isn't working and I think I should be putting each file into its own vector.

string get_file(string filename)
{
ifstream in(filename);
if (in)
{
return (string((istreambuf_iterator<char>(in)), istreambuf_iterator<char>()));
}
}

int main()
{
int numFiles;
string fileName;
cout << "How many files are there?";
cin >> numFiles;
vector<string> A;
for (int i = 0; i < numFiles; i++)
{
cout << "Enter the file name: ";
cin >> fileName;
A[i] = get_file(fileName);
cout << "characters entered: " << A[i];
}



}
You create the A with default constructor. Empty vector.
Then you try to dereference non-existent elements.

You could create empty as now, but then A.reserve(numFiles); and emplace_back values.
That helped with that issue. Now I just need to figure out how to iterate through each element in that vector. Is that possible?
Create a map of numbers and a count of how many files they appear in. Then afterwards print any that appeared in more then half the files.

e.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <map>

using namespace std;

int main ()
{
  map<unsigned, unsigned> int_counter;

  int_counter[1] = 5;
  int_counter[2] = 4;
  int_counter[3] = 3;
  int_counter[4] = 2;
  int_counter[5] = 3;
  int_counter[20] = 6;
  int_counter[21] = 7;

  for (auto iterator : int_counter) {
    if (iterator.second > 3)
      cout << iterator.first << " appears in more than 3 files" << endl;
  }

  return 0;
}


Output is:

1 appears in more than 3 files
2 appears in more than 3 files
20 appears in more than 3 files
21 appears in more than 3 files
Topic archived. No new replies allowed.