June 2017
Intermediate to advanced
532 pages
12h 59m
English
How a trie works was explained in the last recipe, but how we fill it and how we query it looks a bit strange here. Let's have a closer look at the code snippet that fills the empty trie with the content of the text database file:
fstream infile {"db.txt"};for (string line; getline(infile, line);) { istringstream iss {line}; t.insert(istream_iterator<string>{iss}, {});}
The loop fills the string line with the content of the text file, line by line. Then, we copy the string into an istringstream object. From such an input stream object, we can create an istream_iterator, which is useful because our trie does not only accept a container instance for looking up subtries but also primarily iterators. This way, we do not need ...
Read now
Unlock full access