Logistics
• Project Standard Template Library II – Part 1 (clock and design) due Sunday, Sept 25th – Part 2 (water) due Sunday, Oct 16th – Partners for Parts 2-3 • Questions?
Logistics Exam 1
• Important date: • Review session – THURSDAY is Exam 1 – Wednesday, Sept 28th – Will cover: • C++ environment / architecture – 9-10am / 8-9pm • C++ variables, pointers, references – 70-3435 • Aggregates (Arrays, struct) static, const • Makefiles – Will not cover • Classes • Operator overloading • Constructors, Destructors, operator= • Templates
Before we begin The Plan
• Any questions? • Today: STL 1 • Wednesday: STL 2 • Thursday: Exam 1
1 The Standard Template Library STL Components
• A general-purpose C++ library of • containers algorithms and data structures – classes that hold stuff • Based on a concept known as generic • iterators programming – Used to iterate through containers • Implemented by means of the C++ template mechanism – Generalization of C++ pointers • Part of the standard ANSI C++ library • generic algorithms • util package for C++ – Templated functions
STL Components Plan for Today
• function objects (Functors) • More complex containers – Objects that overload operator(); – Substitute for pointers to functions • Algorithms • adaptors • Function – adapt other components to special purposes. – Queues and stacks are adaptors • Allocators – encapsulate a memory model. – decouple the algorithms from assumptions about a particular model.
Sorted Containers Sorted Containers
• Objects are maintained in sorted order set
s = 0 1 2 3 4 5 6 7 8 9 10 12 14 16 18 ms = 0 0 1 2 2 3 4 4 5 6 6 7 8 8 9 10 12 14 16 18
2 Associative Containers Maps
• Associates a key object with a value object map
(Dictionary) mymap.insert(value_type(string("January"), 31)); • Container holds a pair of objects mymap.insert(value_type(string("February"), 28));
– Accessed via predefined value_type map
typedef Maps
• Means to define a new typename typedef map
monthmap::iterator it = mymap.find (string “January”); monthmap:: value_type V = (*it);
cout << V.first; // prints out key (January) cout << V.second; //prints out value (31)
Maps Maps
• Of course, one can always use the [] typedef map
monthmap::iterator it = mymap.find (string “January”); monthmap:: value_type V = (*it);
cout << V.first; // prints out key (January) cout << V.second; //prints out value (31)
3 Bitsets Bitsets
• space-efficient support for sets of bits. bitset<16> b1("1011011110001011"); bitset<16> b2; • operator[] overloaded to provide access to individual bits b2 = ~b1;
• NOT the same as a vector of bool for (int i = b2.size() - 1; i >= 0; i--)
cout << b2[i];
strings basic_string
• strings are actually the char instantiation • Provides capabilities: of the STL basic_string template –Compare (which is a container class). – Append – Assign template
Other containers Algorithms
• There are others: • A set of commonly used templated – See online docs for full list functions. • Many work on container objects – Iterators passed in to indicate positions within • Questions? containers
4 Algorithms Algorithms template
Abridged Catalogue of Abridged Catalogue of algorithms algorithms • Filling & generating • Searching & replacing – Fills or a range with a particular value (constant or – find, find_if, find_first_of, replace, generated) replace_if • Takes predicate as argument! FUNCTOR – fill, fill_n, generate, generate_n – max_element, min_element • Counting – search (range searches) – count, count_if (counts elements w/a given value) • Comparing ranges – equal, mismatch • Manipulating sequences – copy, reverse, swap, random_shuffle • Removing elements – remove, unique (removes duplicates)
Abridged Catalogue of Functors algorithms • Sorting • Function Objects (or Functors) – sort, partial_sort, nth_element – Objects that represent functions – binary_search, lower_bound, upper_bound – merge – Overrides operator() – set_union, set_difference, set_intersection – Functors are Templated • Applying an operation to each element – for_each, transform – But why bother when you have functions? • Takes operation as argument! FUNCTOR! • Remember maps • Numeric Algorithms map
5 Functors Functors template
Functors Functors
• STL Arithmetic functors // Set up a vector vector
Functors and Algorithms Questions
• Note that generic algorithms do not care if the operation argument is – A function – A pointer to a function – Functor
• But why use STL?
6 Top 5 Reasons to use STL Top 5 Reasons to use STL
"/opt/SUNWspro/SC5.0/include/CC/./algorithm.cc", line 5. Source, 2K / Executable 1.5M 1015: Error: The operation "std::list
Top 5 Reasons to use STL But seriously…
5. Source, 2K / Executable 1.5M • Lots of functionality 4. Who needs understandable compiler errors? • Efficiency 3. Who needs understandable linker errors? – very good performance at low run-time space cost – Generalized algorithms are only provided when their 2. Why make your program look overly complicated efficiency is good. when STL can do it for you? – The implementation of the containers and algorithms of 1. Now there’s a standard way to access elements the STL is not specified in the standard, but the beyond the bounds of an array! efficiency of each algorithm is.
Reading the docs Summary
• There’s no standard STL docs • Advanced STL Containers –No man for STL • Algorithms – Except, perhaps the ANSI spec • Functors – Docs on webpage • From STL vendors – Roguewave • Questions? – SGI – categorizes templates • Thursday: Exam 1
7