Upload
branden-fields
View
214
Download
0
Embed Size (px)
Citation preview
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
CS 403: Programming Languages
Lecture 24
Fall 2003
Department of Computer Science
University of Alabama
Joel Jones
Lecture 24 2
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Overview Announcements
Talk today Colloquim tomorrow at 11AM in Houser 108, Jiageng Li,
University of Alabama, Integrated Authorization for Grid System Environments
Review Session next class Study Guide MP3 questions Evaluation Forms The STL from a comparative programming
language standpoint
The Computer Science Department, CS Advisory Board,
and ACM Student Chapter
present
Mike ThomasCIO, Gulf States Paper
“Computer Science in Heterogenous, Multidimensional Business
Environments”
5:00-6:00 PM, Tuesday, Dec. 2nd, EE 119
Pizza & drinks served.
Lecture 24 4
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Collections
Almost all programming languages support aggregations of primitive types in some way C has structs, unions, arrays
Pair Up:
What way of aggregating does Smalltak have?
What way of aggregating does Scheme have?
Lecture 24 5
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Closures and aggregation Aggregation is just a simple data type
mechanism—what else is needed to have concise powerful? A lower syntactic overhead way of specifying new
behavior that works over an aggregate data structure Smalltalk: myCollection do: [ code ] Scheme: (map myList (lambda code )) Works because the language provides a way of
specifying functions “in-place”
Lecture 24 6
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
C++ Templates
Parameterized data types Type-safe macros Come in two types
Class Templates Function Templates
Lecture 24 7
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Class Templates
Can specialize with class (see listing #1) template <class T> class Stack… Stack<class Message>
Or any other type Stack<int>
Lecture 24 8
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Class Template Member Functions
Method body must be in .h file
Lecture 24 9
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Using a Class Template
See listing #2 What is the output?
Lecture 24 10
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Function Templates
Specialize a function to take polymorphic arguments Template <class T> T max(T a, T b)
But be careful with pointer types… See listing #3 What does it print?
Lecture 24 11
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Template Function Specialization
To fix “problem”, use type-specific version: template<> char* max(char* a, char* b){ return strcmp(a, b) > 0 ? a : b; }
Lecture 24 12
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Standard Template Library (STL) Good reference at:
http://www.sgi.com/tech/stl/
Generic Programming specify algorithms and data structures that work with
any data type
Core Components Containers Algorithms Iterators
Lecture 24 13
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
STL: Containers Data structures that manage a set of
memory locations Doesn’t contain many member functions Creating, copying, destroying, adding,
removing No pointers to elements Algorithms do the “day-to-day” stuff
Lecture 24 14
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
STL: Iterators
Used to traverse elements of containers Uniform set/naming across containers Algorithms designed to work with a
particular iterator categoryRandom Access
Bi-Directional
Forward
Output Input
Lecture 24 15
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
STL: Algorithms Decoupled from containers Parameterized by iterator types Algorithm categories:
Non-mutating sequence operations Mutating sequence operations Searching and Sorting Set Operations Heap operations Numeric operations Miscellaneous
Lecture 24 16
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Orthogonal Component Structure So how does this all work together?
vector<int> v(3);v[0] = 7;v[1] = v[0] + 3;v[2] = v[0] + v[1];reverse(v.begin(), v.end());
So what kind of components are v, v.begin(), and reverse?
Algorithm Iterator Container
Lecture 24 17
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Example STL: deque Double-Ended QUEue Supports:
Random access to elements Constant time insertion & removal of elements @ end Linear time insertion and removal of elements in the middle Constant time insertion & removal of elements @ beginning
What role would the template parameter to deque fulfill?
Lecture 24 18
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Example STL: deque Use:
deque<int> Q;Q.push_back(3);Q.push_front(1);Q.insert(Q.begin() + 1, 2);Q[2] = 0;copy(Q.begin(), Q.end(), ostream_iterator<int>(cout, “ “));
What does this do?
Lecture 24 19
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Example STL: stack<T, Sequence>
Adaptor that supports restricted subset of Container functionality Insertion, removal, and inspection of element
at the top of the stack Does not allow iteration through its elements
Lecture 24 20
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Example STL:stack<T, Sequence> Example use:
int main() { stack<int> S; S.push(8); S.push(7); S.push(4); assert(S.size() == 3); assert(S.top() == 4); S.pop(); assert(S.top() == 7); S.pop(); assert(S.top() ==8); S.pop(); assert(S.empty());}
Lecture 24 21
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Containers Sequences
vector, deque, list, slist, bit_vector
Associative Containers set, map, multiset, multimap, hash_set, hash_map,
hash_multiset, hash_multimap, hash
String package char_traits, basic_string, rope (not STL)
Container adaptors stack, queue, priority_queue, bitset
Lecture 24 22
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Algorithms Non-mutating algorithms
for_each, find,, count, mismatch, equal, search Mutating algorithms
copy, swap, transform, replace, fill, generate, remove, unique, reverse, rotate, random_shuffle, random_sample, partition, stable_partition, sorting, nth_element, binary search, merge, set operations heap operations, min and max, lexicographical_compare, permutations
Generalized numeric algorithms iota, accumulate, inner_product, partial_sum,
adjacent_difference, power
Lecture 24 23
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Iterators Iterator classes
istream_iterator ostream_iterator front_insert_iterator back_insert_iterator insert_iterator reverse_iterator reverse_bidirectional_iterator raw_storage_iterator sequence_buffer