Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of...
-
Upload
juliana-anderson -
Category
Documents
-
view
215 -
download
2
Transcript of Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of...
Spring 2010 Advanced Programming
Section 1-STL
Computer Engineering DepartmentFaculty of EngineeringCairo University
Advanced ProgrammingSpring 2010
Standard Template Library (STL)
By Eng. Yasmine Badr
Adapted from
www.tantalon.com/pete/embracingstl.ppt
Spring 2010 Advanced Programming
Section 1-STL
Agenda
• Introduction• Containers• Iterators• Algorithms• Functors
Spring 2010 Advanced Programming
Section 1-STL
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 parameterizedalmost every component in STL is a template.
Spring 2010 Advanced Programming
Section 1-STL
Advantages
• Standardized• Thin & efficient• Little inheritance; no virtual functions• Small; easy to learn• Flexible and extensible• Naturally open source
Spring 2010 Advanced Programming
Section 1-STL
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
Spring 2010 Advanced Programming
Section 1-STL
Containers (cont.)
Container Description Keysvector dynamic array
deque dynamic array -- both ends
list linked list
set sorted list of keys no duplicate keys
map sorted list of key and value pairs no duplicate keys
multiset sorted list of keys duplicate keys OK
multimap sorted list of key and value pairs duplicate keys OK
valarray similar to vector<T>, where T is numeric, but optimized for numeric ops
bitset similar to vector<bool>, but fixed size and includes bit operators
Spring 2010 Advanced Programming
Section 1-STL
Vector
• Dynamic array, Contiguous block of memory• reserve(), capacity()• Insert invalidates all iterators if 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).
Spring 2010 Advanced Programming
Section 1-STL
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 iterators to
begin/end
Spring 2010 Advanced Programming
Section 1-STL
List
• Doubly-linked list• Fast insert/erase; no random access• Special functions: splice(), merge()• Erase invalidates iterators to erased elements;
insert never invalidates any iterators
Spring 2010 Advanced Programming
Section 1-STL
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
Spring 2010 Advanced Programming
Section 1-STL
Map
• Dictionary of sorted elements• List of sorted key and value pairs
Spring 2010 Advanced Programming
Section 1-STL
Container Adaptors
• Implemented by using the fundamental containers classes.
• Example adapator code
stack<int, deque<int> > stackDeq;stackDeq.push(1);int i = stackDeq.top();stackDeq.pop();
Adaptor Example containers Default container
stack list, deque, vector deque
queue list, deque deque
priority_queue list, deque, vector vector
Spring 2010 Advanced Programming
Section 1-STL
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>::iterator i;for (i = c.begin(); i != c.end() ++i) // forward T t = *i;
for (i = c.rbegin(); i != c.rend() ++i) // backward T t = *i;
Spring 2010 Advanced Programming
Section 1-STL
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(), ...)
Spring 2010 Advanced Programming
Section 1-STL
Algorithms (cont.)
• Examples:
#include <algorithm>
// return num elements equal to 123int i = count(c.begin(), c.end(), 123);
// negate all elementstransform(c.begin(), c.end(),d.begin(), negate<int>());
// apply myFn to all elementsfor_each(c.begin(), c.end(), myFn);
// shuffle the deckrandom_shuffle(deck.begin(), deck.end());
Spring 2010 Advanced Programming
Section 1-STL
Function Objects (Functors)
• C++ objects that can be called like a function to implement “callbacks”
• Use C++ operator()(...)
Spring 2010 Advanced Programming
Section 1-STL
Functors (cont.)
• Functors that do ordering are called “predicates”
• Example:
struct StlStrPred // “public” class{ bool operator()(const char* a, const char* b) { return (strcmp(a, b) == -1); } };
vector<char*> v;sort(v.begin(), v.end(), StlStrPred());