SlideServe
Menu
Préférences
© SlideServe · v2026

Advanced Programming Spring 2010

guy-glover avatar
17 diapositives · 307 Views · Oct 28, 2014

This presentation on the Standard Template Library (STL) provides an insightful introduction to one of the most essential components of C++ programming. It covers key concepts including STL containers, iterators, algorithms, and functors. The STL offers standardized and efficient data structures and algorithms, maximizing the flexibility and extensibility of C++ programs. Learn about crucial containers like vectors, deques, lists, sets, and maps, and explore how iterators facilitate element access. Understand the power of algorithms for searching, sorting, and transforming data, as well as the role of functors in C++.

Advanced Programming Spring 2010 - slide 1
01 / 17Advanced Programming Spring 2010
1 / 17

Télécharger la présentation

Advanced Programming Spring 2010
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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());