C++11 Stream Iterators feel like Python
The C++11 standard has plenty of new goodies that make programmer’s life easier without sacrificing rigor.
Here is a neat example from the book “The C++ Programming Language” by Bjarne Stroustrup,
that in its 4th edition covers the C++11 standard.
Found in Page 107, Section 4.5.3 Stream Iterators.
Let’s consider the tasks of:
- Reading a list of words from an input file
- Sorting them
- Eliminating duplicates and
- Writing them down into an output file
This can be done with the classic Unix shell commands:
cat inputfile | sort | uniq > outputfile |
Let the code (and Stroustrup) speak:
#include <iterator> |
This code
- Reads two filenames from the standard input
- Opens one file for input
- Opens one file for output
- Associates one iterator to the input file
- Associates one iterator to the output file, along with a separator “\n”
- Creates a vector to contain strings and attach it to the iterator of the input file
- At this point the full file is read into memory and placed into the vector of strings
- Calls std::sort and in the process trigger the read.
- Once sorted, copies unique entries to the output file, using the output iterator
This can be rewritten a bit shorter (from the same Book, page 108) as:
#include <iterator>
|
import sys
infile = open(str(sys.argv[1])) outfile.writelines(sorted(list(set( infile.readlines() )))) infile.close() |
import this |
that returns the Zen of Python, by Tim Peters:
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren’t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one– and preferably only one –obvious way to do it. Although that way may not be obvious at first unless you’re Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it’s a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea — let’s do more of those! |
There is a lot in here that C++ and Python developers can agree upon.
Very nice.
An extra line can be removed :p See https://github.com/luisibanez/Cxx11/pull/1
You are quite right !
Your improvement has now been merged:
https://github.com/luisibanez/Cxx11/commit/680cdfe72d65314af5f240aa058523fed26af20e
Thanks !
And ‘string’ header can also be removed. See https://github.com/luisibanez/Cxx11/pull/2
I really love C++11, but above examples barely show something new. Stream iterators existed before, (see, e.g. Josuttis “The C++ Standart Library” Addison-Wesley 1999).
Replace the “using” by “typedef” and the “{}” by (), i.e. the initializer lists by the constructors, and you get a well-formed c++98 program that doesn’t look very different, i.e.
#include
#include
#include
#include
#include
#include
int main(int argc, const char * argv [] )
{
std::ifstream inputfile ( argv[1] );
std::ofstream outputfile ( argv[2] );
typedef std::istream_iterator< std::string > istritr;
typedef std::ostream_iterator< std::string > ostritr;
std::set< std::string >
words( (istritr(inputfile)), istritr() );
std::copy( words.begin(), words.end(),
(ostritr( outputfile, “n” )));
return !inputfile.eof() || !outputfile;
}
regards,
Gert,
Thanks for the clarification and your C++98 vs C++11 correction.
Your point is well taken.