Hacker News new | ask | show | jobs
by abelsson 4625 days ago
Hah, I was just about to post more or less exactly the same code. :)

It's funny, had the author tweaked the problem just slightly to try make C++ look good by saying that the program should output sorted words instead of lines, you could have deleted the entire Line nonsense. Had the problem been to output sorted, unique words, you could have made the rather elegant:

    int main(int argc, const char* argv[]) 
    {
      using input = istream_iterator<string>;
      using output = ostream_iterator<string>;
    
      ifstream inputfile(argv[1]);
      ofstream outputfile(argv[2]);
    
      set<string> words{input(inputfile), input()};
      copy(words.begin(), words.end(), output(outputfile, "\n"));
    }
But then again, it's not exactly in C++'s favor as a scripting language that copying words is easy, while lines is hard.
1 comments

Yeah, it is not a scripting language for processing lines. Although the line nonsense has a benefit though it could be adapted to parse out records and by overloading the less than operator to sort on a specific column. But then you would might use AWK instead.