logistics
play

Logistics Project Standard Template Library II Part 1 (clock and - PDF document

Logistics Project Standard Template Library II Part 1 (clock and design) due Sunday, Sept 25 th Part 2 (water) due Sunday, Oct 16 th Partners for Parts 2-3 Questions? Logistics Exam 1 Important date: Review


  1. Logistics • Project Standard Template Library II – Part 1 (clock and design) due Sunday, Sept 25 th – Part 2 (water) due Sunday, Oct 16 th – Partners for Parts 2-3 • Questions? Logistics Exam 1 • Important date: • Review session – THURSDAY is Exam 1 – Wednesday, Sept 28 th – Will cover: – 9-10am / 8-9pm • C++ environment / architecture • 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

  2. 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++ – Generalization of C++ pointers template mechanism • generic algorithms • Part of the standard ANSI C++ library – Templated functions • util package for C++ STL Components Plan for Today • function objects (Functors) • More complex containers – Objects that overload operator(); • Algorithms – Substitute for pointers to functions • Function • adaptors – 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 set<int, less<int> > s; • Objects are maintained in sorted order multiset<int, less<int> > 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

  3. Associative Containers Maps map<string, int, less<string> > mymap; • Associates a key object with a value object (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<string, int, less<string>>::iterator it = mymap.find (string “January”); • Examples map<string, int, less<string>>::value_type V = (*it); – map cout << V.first; // prints out key (January) cout << V.second; //prints out value (31) typedef Maps typedef map<string, int, less<string> > monthmap; • Means to define a new typename 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 typedef map<string, int, less<string> > monthmap; • Of course, one can always use the [] 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

  4. Bitsets Bitsets bitset<16> b1("1011011110001011"); • space-efficient support for sets of bits. 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 – Insert template <class charT, class traits = char_traits<charT>, – Remove class Allocator allocator<charT> > – Replace class basic_string; – various searches typedef basic_string <char> string; – Iterator access Other containers Algorithms • There are others: • A set of commonly used templated functions. – See online docs for full list • Many work on container objects – Iterators passed in to indicate positions within containers • Questions? 4

  5. Algorithms Algorithms list<int> nums; template <class Iterator, class T> list<int>::iterator nums_iter; Iterator find (Iterator first, Iterator 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 • Searching & replacing • Filling & generating – find, find_if, find_first_of, replace, – Fills or a range with a particular value (constant or replace_if generated) • Takes predicate as argument! FUNCTOR – fill, fill_n, generate, generate_n – max_element, min_element – search (range searches) • Counting • Comparing ranges – count, count_if (counts elements w/a given value) – equal, mismatch • Manipulating sequences • Removing elements – copy, reverse, swap, random_shuffle – 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 – Overrides operator() – merge – Functors are Templated – set_union, set_difference, set_intersection • 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<string, int, less<string> > – Accumulate, partial_sum mymap; 5

  6. Functors Functors template <class T> struct less : public • STL Predicates (returns bool) binary_function<T, T, bool> { equal_to arg1 == arg2 bool operator() (const T &x, const T &y) 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 // Set up a vector • STL Arithmetic functors vector<int> v; plus arg1 + arg2 minus arg1 - arg2 // Setup a function object multiplies arg1 * arg2 out_times_x<int> 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

  7. 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<int, std::allocator<int>>::iterator - std::list<int, 4. Who needs understandable compiler errors? std::allocator<int>>::iterator" is illegal. "/opt/SUNWspro/SC5.0/include/CC/./algorithm", line 776: Where: While instantiating "std::__final_insertion_sort<std::list<int,std::alloc ator<int>>::iterator>(std::list<int, std::allocator<int>>::iterator, std::list<int, std::allocator<int>>::iterator)". "/opt/SUNWspro/SC5.0/include/CC/./algorithm", line 776: Where: Instantiated from non-template code. Top 5 Reasons to use STL But seriously… • Lots of functionality 5. Source, 2K / Executable 1.5M • Efficiency 4. Who needs understandable compiler errors? – very good performance at low run-time space cost 3. Who needs understandable linker errors? – 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 • Questions? – Roguewave – SGI – categorizes templates • Thursday: Exam 1 7

Recommend


More recommend