The main routine initializes the prefix, reads the input(from standard input, called cin in the C++(www.cppentry.com) iostream library), adds a tail, and generates the output, exactly as in the earlier versions:
The function build uses the iostream library to read the input one word at a time:
The string buf will grow as necessary to handle input words of arbitrary length.
The add function shows more of the advantages of using the STL:
Quite a bit is going on under these apparently simple statements.The map container overloads subscripting (the [] operator) to behave as a lookup operation. The expression statetab [prefix] does a lookup in statetab with prefix as key and returns a reference to the desired entry; the vector is created if it does not exist already. The push_back member functions of vector and deque push a new string onto the back end of the vector or deque; pop_front pops the first element off the deque.