Presentation is loading. Please wait.

Presentation is loading. Please wait.

Duke CPS 108 7. 1 Iterators: Patterns and STL l Access a container without knowing how it’s implemented ä libtapestry: first, isDone, next, current iterators.

Similar presentations


Presentation on theme: "Duke CPS 108 7. 1 Iterators: Patterns and STL l Access a container without knowing how it’s implemented ä libtapestry: first, isDone, next, current iterators."— Presentation transcript:

1 Duke CPS 108 7. 1 Iterators: Patterns and STL l Access a container without knowing how it’s implemented ä libtapestry: first, isDone, next, current iterators are part of an inheritance hierarchy: Iterator ä STL begin, end, *, ++ for pointer like syntax no inheritance, all typedefs in each STL class l What are iterator properties, who makes the iterator ä const, non-const, random-access, … ä makeIterator uses new internally, who deletes? Pointer Proxy: “smart pointers”, allocated on stack but act like heap-allocated pointers

2 Duke CPS 108 7. 2 Access to map classes l Both STL and libtapestry use a templated Pair class ä first, second in pair correspond to key, value in map  returned by *iterator and iterator->current() l libtapestry ä includes, insert, getValue: see map.h ä leads to redundant lookups in map implementation l STL ä operator[], insert  map[key] = value; inserts pair if not there  potential problem with if(map[key] == …) l hash_map is NOT standard STL, map uses red-black tree

3 Duke CPS 108 7. 3 templates and generic classes/functions l STL is the standard template library ä part of C++ as a library of generic functions, classes, and algorithms ä uses no inheritance, but templates can require existence of operators/functions in hash_map class, operator == is needed in map class, operator < is needed l A templated class is not code, but a code generator ä separation of interface and implementation is tricky ä implementation is a generator, not code ä must instantiate generator at compile time

4 Duke CPS 108 7. 4 Using templated functions (classes) l Generic sort template void sort(Vector & a, int numElts) // pre: numElts = number of elements in a // post: a[0] <= a[1] <= … <= a[numElts-1] l What must be true of Type? ä Comparable ä ??? l Instantiate templated function/class ä bind template parameters, unify types Vector vs; … sort(vs,vs.size()); Vector > vvi; … sort(vvi,vvi.size());

5 Duke CPS 108 7. 5 templated classes l If interface (.h) and implementation (.cc) are separate, client code must have access to implementation ä can define all code inline, in class (vector.h) ä can explicitly instantiate all uses in a separate file ä can #include “tempclass.cc” in.h file, problems? -frepo flag fixes this in g++ VC++ (and other PC compilers) use this approach l STL classes use inline approach ä see the or header files l Dangers of code-bloat  no literal code sharing between Vector and Vector

6 Duke CPS 108 7. 6 Where are bottlenecks? l You can time sections of code ä class Ctimer accessible via “ctimer.h” ä you can use /bin/time or shell time real, system, user l You can profile code ä use the -pg option with compiler and run gprof (see help page) ä instruments code, finds how many times each function is called, what percentage of overall time each uses l You can optimize code ä -O2, -O4 with g++, this can affect debugging ä make it run, make it right, make it fast


Download ppt "Duke CPS 108 7. 1 Iterators: Patterns and STL l Access a container without knowing how it’s implemented ä libtapestry: first, isDone, next, current iterators."

Similar presentations


Ads by Google