Skip to main content
edited body; added 71 characters in body; added 6 characters in body
Source Link
Incomputable
  • 9.7k
  • 3
  • 34
  • 73

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.

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 1 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.

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 2 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.

deleted 259 characters in body
Source Link
Incomputable
  • 9.7k
  • 3
  • 34
  • 73

Bug:

After first iteration, you're opening the same file as answers stream is opened from, which actually renders next iterations literally useless.

    candidates.open(argv[1], ios::in);
    //                   ^^ should be 2, not 1

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 1 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.

Bug:

After first iteration, you're opening the same file as answers stream is opened from, which actually renders next iterations literally useless.

    candidates.open(argv[1], ios::in);
    //                   ^^ should be 2, not 1

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 1 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.

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 1 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.

added edge cases
Source Link
Incomputable
  • 9.7k
  • 3
  • 34
  • 73

Bug:

After first iteration, you're opening the same file as answers stream is opened from, which actually renders next iterations literally useless.

    candidates.open(argv[1], ios::in);
    //                   ^^ should be 2, not 1

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 1 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 > 01)
    {
        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.

Bug:

After first iteration, you're opening the same file as answers stream is opened from, which actually renders next iterations literally useless.

    candidates.open(argv[1], ios::in);
    //                   ^^ should be 2, not 1

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 1 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 > 0)
    {
        std::cout << reading.first << '\n';
    }
}

Bug:

After first iteration, you're opening the same file as answers stream is opened from, which actually renders next iterations literally useless.

    candidates.open(argv[1], ios::in);
    //                   ^^ should be 2, not 1

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 1 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.

Source Link
Incomputable
  • 9.7k
  • 3
  • 34
  • 73
Loading