Iterators: Patterns and STL Access a container without knowing how - - PowerPoint PPT Presentation

iterators patterns and stl
SMART_READER_LITE
LIVE PREVIEW

Iterators: Patterns and STL Access a container without knowing how - - PowerPoint PPT Presentation

Iterators: Patterns and STL Access a container without knowing how its implemented libtapestry: first, isDone, next, current iterators are part of an inheritance hierarchy: Iterator STL begin, end, *, ++ for pointer


slide-1
SLIDE 1

Duke CPS 108

  • 7. 1

Iterators: Patterns and STL

  • 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
  • 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

slide-2
SLIDE 2

Duke CPS 108

  • 7. 2

Access to map classes

  • 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()

  • libtapestry

➤ includes, insert, getValue: see map.h ➤ leads to redundant lookups in map implementation

  • STL

➤ operator[], insert

➤ map[key] = value; inserts pair if not there

➤ potential problem with if(map[key] == …)

  • hash_map is NOT standard STL, map uses red-black tree
slide-3
SLIDE 3

Duke CPS 108

  • 7. 3

templates and generic classes/functions

  • 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

  • perators/functions
  • in hash_map class, operator == is needed
  • in map class, operator < is needed
  • 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

slide-4
SLIDE 4

Duke CPS 108

  • 7. 4

Using templated functions (classes)

  • Generic sort

template <class Type> void sort(Vector<Type> & a, int numElts) // pre: numElts = number of elements in a // post: a[0] <= a[1] <= … <= a[numElts-1]

  • What must be true of Type?

➤ Comparable ➤ ???

  • Instantiate templated function/class

➤ bind template parameters, unify types

Vector<string> vs; … sort(vs,vs.size()); Vector<Vector<int> > vvi; … sort(vvi,vvi.size());

slide-5
SLIDE 5

Duke CPS 108

  • 7. 5

templated classes

  • 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
  • STL classes use inline approach

➤ see the <vector> or <map> header files

  • Dangers of code-bloat

➤ no literal code sharing between Vector<string> and

Vector<int>

slide-6
SLIDE 6

Duke CPS 108

  • 7. 6

Where are bottlenecks?

  • You can time sections of code

➤ class Ctimer accessible via “ctimer.h” ➤ you can use /bin/time or shell time

  • real, system, user
  • 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

  • You can optimize code

➤ -O2, -O4 with g++, this can affect debugging ➤ make it run, make it right, make it fast