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<std::string, std::size_t> will fit the job nicely. After you've done with it, just iterate through and see if anything has counter equal to 12 or higher, and print those.
Roughly this:
std::map<std::string, std::size_t> appearance_count;
while (std::getline(answers, s))
{
++appearance_count[s];
}
while (std::getline(candidates, s))
{
++appearance_count[s];
}
for (const auto& reading: appearance_count)
{
if (reading.second > 1)
{
std::cout << reading.first << '\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.