Advanced Programming Spring 2010
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
E N D
Presentation Transcript
Advanced ProgrammingSpring 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; • Provides many of the basic algorithms and data structures. • A generic library, i.e. its components are heavily parameterizedalmostevery component in STL is a template.
Advantages • Standardized • Thin & efficient • Little inheritance; no virtual functions • Small; easy to learn • Flexible and extensible • Naturally open source
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
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).
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
List • Doubly-linked list • Fast insert/erase; no random access • Special functions: splice(), merge() • Erase invalidates iteratorsto erased elements; insert never invalidates any iterators
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
Map • Dictionary of sorted elements • List of sorted key and value pairs
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();
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;
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(), ...)
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());
Function Objects (Functors) • C++ objects that can be called like a function to implement “callbacks” • Use C++ operator()(...)
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());