Logistics

• Project Standard 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: • ++ 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 > s; multiset > ms; • Requires Comparator function • Examples for (int i = 0; i < 10; i++) { s.insert(i); s.insert(i * 2); – Set – Collection of unique values ms.insert(i); ms.insert(i * – Multiset – Collection of non-unique values 2); }

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 > mymap;

(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>::iterator it = • Examples mymap.find (string “January”); map>::value_type V = (*it); –map cout << V.first; // prints out key (January) cout << V.second; //prints out value (31)

Maps

• Means to define a new typename typedef map > monthmap; monthmap mymap; • Makes Template types more manageable mymap.insert(value_type(string("January"), 31)); typedef definition typename mymap.insert(value_type(string("February"), 28));

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; monthmap mymap; operator to make life easier… mymap [“January"] = 31; mymap [“February"] = 28;

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 , – Remove class Allocator allocator > – Replace class basic_string; – various searches typedef basic_string string; – Iterator access

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 list nums; Iterator find (Iterator first, Iterator list::iterator nums_iter; last, const T & value) nums.push_back (3); { nums.push_back (7); for (Iterator i = first; i != last && nums.push_front (10); *i != value; ++i); return i; // Search the list nums_iter = find(nums.begin(), nums.end(), 3); } if (nums_iter != nums.end()) { /* found */ } else { /* not found */ }

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 > – Accumulate, partial_sum mymap;

5 Functors Functors template struct less : public binary_function { • STL Predicates (returns bool) bool operator() (const T &x, const T &y) equal_to arg1 == arg2 const not_equal_to arg1 != arg2 { return x < y; } greater arg1 > arg2 } less arg1 < arg2 greater_equal arg1 >= arg2 less_equal arg1 <= arg2 Note: will only work if < is defined for T. logical_and arg1 && arg2 logical_or arg1 || arg2 logical_not !arg

Functors Functors

• STL Arithmetic functors // Set up a vector vector v; plus arg1 + arg2 minus arg1 - arg2 // Setup a function object multiplies arg1 * arg2 out_times_x f2(2); divides arg1 / arg2 // Apply function modulus arg1 % arg2 for_each(v.begin(),v.end(),f2); negate -arg1 // Will apply f2 to each element in v // f2.operator() (int) best be defined

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>::iterator - std::list>::iterator" is illegal. "/opt/SUNWspro/SC5.0/include/CC/./algorithm", line 776: Where: While instantiating "std::__final_insertion_sort>::iterator>(std::list>::iterator, std::list>::iterator)". "/opt/SUNWspro/SC5.0/include/CC/./algorithm", line 776: Where: Instantiated from non-template code.

Top 5 Reasons to use STL But seriously…

5. Source, 2K / Executable 1.5M • Lots of functionality 4. Who needs understandable 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