Iterate over a file multiple times Aim of the code is to print all of the strings from one file that match strings from another file. Names of both of the files are provided by command line arguments.

Code:

int main(int argc, char *argv[]){    
    ifstream answers(argv[1]);
    ifstream candidates(argv[2]);


    for (string s; getline(answers,s);){
        for (string h; getline(candidates,h);){
            if (!s.compare(h)){
                cout &lt;&lt; h &lt;&lt; ":" &lt;&lt; s &lt;&lt; endl;
            }
        }
        candidates.close();                 //I know theres better than this
        candidates.open(argv[2], ios::in); 
    }
}


but I feel like reloading the file into memory every time is redundant. Is there anything that could be improved?
Slightly better way:

    candidates.close();                 //I know theres better than this


If improvement is based on this statement, then something like stringstream would be good fit.

Best way:

std::map&lt;std::string, std::size_t&gt; will fit the job nicely. After you've done with it, just iterate through and see if anything has counter equal to 2 or higher, and print those.

Roughly this:

std::map&lt;std::string, std::size_t&gt; appearance_count;
while (std::getline(answers, s)) 
{
    ++appearance_count[s];
}
while (std::getline(candidates, s)) 
{
    ++appearance_count[s];
}

for (const auto&amp; reading: appearance_count)
{
    if (reading.second &gt; 1)
    {
        std::cout &lt;&lt; reading.first &lt;&lt; '\n';
    }
}


Some edge cases:

There might be duplications in the first file, so it will require first adding into std::set, then adding to the map. Second file is unaffected by that, since any string with appearance count larger than 1 is already wanted. Though if the second file contains duplicates as well, you'll need two sets.