
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























n What happens to user when function returns? q It gets cleaned up automatically when the function returns
n From Lab 4B: // Implement + for sparse-vectors. const SparseVector & SparseVector::operator+( const SparseVector &rhs) const { return SparseVector(*this) += rhs; } n Does this work? q This also returns a reference to a local variable. q More subtle, since the variable is an unnamed temporary q Will eventually result in a catastrophic failure of some kind n Simple arithmetic operators should always return a const-object value (e.g. const SparseVector)
n Lab 6 has 13 classes in two header files: q Expression etc. - expressions.hh q Command etc. - commands.hh n Managing relationships between header files can be annoying! q Commands depend on expressions q Other files depend on both of these q Easily leads to “multiple declaration” errors
n Preprocessor directives start with # character n Performs simple text-level processing before the actual compilation phase begins q Inclusion of named files
q Macro substitution
q Conditional compilation
n Enclose contents of include file with a guard #ifndef SOMEFILE_HH #define SOMEFILE_HH ... // Normal header file contents #endif // SOMEFILE_HH q Comment after #endif is just for readability n First time file is included, symbol isn’t defined. q Symbol will be defined on subsequent inclusions, so contents won’t be repeated. n Choose a symbol name that is likely not to be used elsewhere! q Some variant of header file’s name is usually safe
n C/C++ assert() macro uses the preprocessor #ifdef NDEBUG #define assert(e) ((void) 0) #else #define assert(e)
((void) ((e)? 0 : __assert(#e, FILE, LINE))) ... // Definition of __assert() #endif n If NDEBUG symbol is defined during compilation, assert() becomes a no-op q g++ -DNDEBUG ... q -D… defines the specified symbol when compiling
n C/C++ assert() macro: #ifdef NDEBUG #define assert(e) ((void) 0) #else #define assert(e)
((void) ((e)? 0 : __assert(#e, FILE, LINE))) ... // Definition of __assert() #endif n FILE and LINE are special symbols q Managed/updated by the preprocessor q FILE is file currently being compiled (.cc or .hh) q LINE is the current line of file being compiled
n C/C++ assert() macro: #ifdef NDEBUG #define assert(e) ((void) 0) #else #define assert(e)
((void) ((e)? 0 : __assert(#e, FILE, LINE))) ... // Definition of __assert() #endif n Finally, the definition contains no semicolons! n Provided by the caller: assert(curr->value != 0);
n Lab 7 is a brief introduction to the capabilities of the Standard Template Library (“the STL”) n Generate a search index for a text document q Make a list of unique words q Keep a count of each word’s occurrences q Find out if a word appears in a set of stop-words n The STL is a very powerful set of tools for managing and processing information q Definitely want to learn this library!
n First category of containers: Sequences q Use indexing for access q Keep their values in a specific order n vector – a growable array q Constant indexing, linear cost for insert/resize n deque – double-ended queue q Constant prepend/append time, linear insert time n list – a doubly linked list q Constant insert time, linear cost for indexing q Supports forward and backward traversal n slist – a singly linked list q Only supports forward traversal
n The other category: Associative Containers
n set, multiset – ordered collection of keys
n map, multimap – ordered collection of ( key , value ) pairs
n hash_set, hash_multiset – unordered collection of keys
n hash_map, hash_multimap – unordered (key, value) pairs
n set<string, greater
n Want to generate a search-index from a text document n What does program need to keep track of? q A mapping from each word we’ve seen, to the number of times that word appears n Can use the STL map template: #include