Exercise 5
About | Files | Solutions |
---|---|---|
Iterators, lambdas, standard containers. Stream error handling. | exercise5.tar.gz | sol-exercise5.tar.gz |
- The class
Text
represents texts that have several lines. The class has no constructor, no destructor, no copy constructor and no assignment operator. Are these functions not necessary? Supply the class with an iterator so the program in testtext.cc functions correctly. You must make additions to the class, but you may not change the representation of the text. - The program in personsorttest.cc gives a lot of compilation errors. What is the cause of the errors? Fix the program.
- Modify the program in sortwords.cc so it functions as described in the comments.
- When you wish to store values that are identified by keys you usually would use a map. A map has logarithmic time complexity for insertions, deletions and searches and is normally implemented with a binary search tree. This has some drawbacks -- for example, a map iterator is complex (it must traverse a tree), and the nodes in the tree need space for pointers to children and parent.
Many applications that use a map are of the type "insert values at the start, then search many times." Then, it would be better to use a sorted vector to store the values (the insertions will be slower but searching will be faster, and the values will occupy less space).
Write a classAV
(for Associative Vector) that functions as astd::map
but stores the values in a sorted vector. Binary search should be used for all searches (there are binary search algorithms in the standard library). You only need to implement the constructs that are used in avtest.cc. - Error handling is both important and slightly complicated when it comes to reading streams.
Study the program istream_iterator_test.cc.
In the input streamss
, there are integral numbers and other strings, so when using anistream_iterator<int>
, it will stop when the first item that is not an integral number is encountered.
Add error handling so that the program skips the strings that cannot be converted to integers.
Change your program so that it prints:
1
17
[ss: not an int: kalle]
2
[ss: not an int: nisse]
3
[ss: not an int: pelle]
ss.eof(): true
Note thatistream::eof()
means that the program has tried to read beyond the end-of-file, not that the next symbol is end-of-file.
- Error handling is both important and slightly complicated when it comes to reading streams.