3 ms·
Agreed that C++11 is better than C++Original. But no. It's still 19 non-blank lines, all of which you have to think about and maintain. Compare this to an idiom
by benhoyt 13y ago
Agreed that C++11 is better than C++Original. But no. It's still 19 non-blank lines, all of which you have to think about and maintain. Compare this to an idiomatic version written in a "real" scripting language like Python.
import fileinput, sys
for line in sorted(fileinput.input()):
sys.stdout.write(line)
Advantages over C++:
* Fewer import/using lines (5 lines in the C++ version).
* No variable declarations (4 lines in the C++ version).
* Automatic iteration over file or file-like objects is very nice. No need to build a list via getline() and the terribly-named "push_back()" function.
* No "data.begin(), data.end()" parameters to the sort function -- sane defaults, people.
* "for line in file" is so much easier to read than "for(const auto &i : data)".
* Return 0 is implicit. Explicit is better than implicit, I know, but this is a very sane default. If there's an exception, Python won't return 0.
* Better automatic error handling. What does the C++ version print if a file doesn't exist or if there's a read error?
* Thanks to Python's standard library (fileinput module), it automatically handles stdin, multiple input files, etc.