1 / 17

Advanced Programming Spring 2010

Advanced Programming Spring 2010. Standard Template Library (STL). Adapted from www.tantalon.com/pete/ embracingstl .ppt. Agenda. Introduction Containers Iterators Algorithms Functors. STL. Standard C ++ library of: container classes , algorithms, and iterators ;

guy-glover
Télécharger la présentation

Advanced Programming Spring 2010

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced ProgrammingSpring 2010 Standard Template Library (STL) Adapted from www.tantalon.com/pete/embracingstl.ppt

  2. Agenda • Introduction • Containers • Iterators • Algorithms • Functors

  3. STL • Standard C++ library of: • container classes, • algorithms, and • iterators; • Provides many of the basic algorithms and data structures. • A generic library, i.e. its components are heavily parameterizedalmostevery component in STL is a template.

  4. Advantages • Standardized • Thin & efficient • Little inheritance; no virtual functions • Small; easy to learn • Flexible and extensible • Naturally open source

  5. Containers • Containers contain elements; they “own” the objects • Containers provide iterators that point to its elements. • Containers provide a minimal set of operations for manipulating elements

  6. Containers (cont.)

  7. Vector • Dynamic array, Contiguous block of memory • reserve(), capacity() • Insert invalidates all iteratorsif capacity changes • Vectors good at: • Accessing individual elements by their position index (constant time). • Iterating over the elements in any order (linear time). • Add &remove elements from its end (const. time).

  8. Deque • Double-ended queue • Fast ins/erase at begin and end • Insert invalidates all iterators • Erase in middle invalidates all iterators • Erase at begin/end invalidates iteratorsto begin/end

  9. List • Doubly-linked list • Fast insert/erase; no random access • Special functions: splice(), merge() • Erase invalidates iteratorsto erased elements; insert never invalidates any iterators

  10. Set • List of sorted elements • Fast retrieval based on key (log N) • Fast insert/erase (log N) • Red-black tree (balanced 2-3-4 tree) • Erase invalidates erased elements • Insert never invalidates any iterators

  11. Map • Dictionary of sorted elements • List of sorted key and value pairs

  12. Container Adaptors • Implemented by using the fundamental containers classes. • Example adapator code stack<int, deque<int> > stackDeq; stackDeq.push(1); inti = stackDeq.top(); stackDeq.pop();

  13. Iterators • Generalization of pointers: they are objects that point to other objects • Often used to iterate over a range of objects: • Typical iteration c<T>::iteratori; for (i = c.begin(); i != c.end() ++i) // forward T t = *i; for (i = c.rbegin(); i != c.rend() ++i) // backward T t = *i;

  14. Algorithms • Approx. 60 standard algorithms • searching (e.g. find()) • sorting (e.g. sort()) • mutating (e.g. transform()) • Most functions take the form: • fn(c.begin(), c.end(), ...)

  15. Algorithms (cont.) • Examples: #include <algorithm> // return num elements equal to 123 inti = count(c.begin(), c.end(), 123); // negate all elements transform(c.begin(), c.end(),d.begin(), negate<int>()); // apply myFn to all elements for_each(c.begin(), c.end(), myFn); // shuffle the deck random_shuffle(deck.begin(), deck.end());

  16. Function Objects (Functors) • C++ objects that can be called like a function to implement “callbacks” • Use C++ operator()(...)

  17. Functors (cont.) • Functors that do ordering are called “predicates” • Example: structStlStrPred// “public” class { bool operator()(const char* a, const char* b) { return (strcmp(a, b) == -1); } }; vector<char*> v; sort(v.begin(), v.end(), StlStrPred());

More Related