CS60 Lecture 23 ------------------------ Announce ----------------- - proj 5 due tommorrow @ midnight This Week ----------------- - the STL Today ----------------- - Review - STL Concepts Review ----------------- - Encapsulation, Inheritance, Polymorphism - Friends, Overloaded ops, Templates, Exceptions - friends: efficiency - templates: generic code - overloaded ops: generic code + convenience - exceptions: convenience The STL -------------------- - Standard Template Library - standard generic library for data structures and algorithms (ALL TEMPLATES!) - imagine the world without, how many DIFFERENT stack classes would exist?! - has 'container' classes and 'algorithms' along with supporting objects/concepts to tie them all together - goal not only to write code once, but provide uniform access AMONG data/algorithms in the STL...and efficient :) Uniformity among the STL containers -------------------------------------- - STL contains container classes for stack, queue, priority_queue, list, vector, dqueue, set, map... - note taht all of these are one dimensional (some sequential) data structures - some commonalities exist as to how programmers access one dimensional data structures - for instance, how do we access arrays? int foo[100]; - we use an index of course, but what are we really doing? moving forwards and backwards through the array. - we move forward and/or backward through a list, a set, a vector - why not overload the index operator? - some do, but index op is dangerous (segfault) - some types may have values whose index changes - ll example where node in middle is removed - due to the fact that we're dealing with ADTs, we are not allowed access to private data so we can't index directly. - some data structures only allow one way access - some data structures are internally stored in somthing other than a flat array - we can 'move forward' or 'move backward' but it isn't as simple as moving an index variable + or - 1 internally - They came up with notion called 'iterator', like a pointer but is a class - all seq. container classes in STL have an associated iterator - the vector: dynamic array include using namespace std; main() { vector vints; for (i=0; i<10; i++) { vints.push_back(i); } for (i=0; i<10; i++) { cout << vints[i] << " "; } } - OR we can use an iterator vector::iterator p; for (p=vints.begin(); p != vints.end(); p++) { cout << *p << " "; } cout << endl; - what is happening - well, we have declared a variable 'p' that is of type vector::iterator - template classes can actually have internal 'types' defined, which is what this is - we are using vector member functions 'begin() and end(). - begin() returns an iterator which points to the first index of vector - end() can be thought of as 'one element past the array' - we are using two overloaded iterator operators '++' and '*'. - ++ moves the iterator along by one - * 'dereferences' the iterator, just like a pointer - many STL container classes have iterators, but not all support all operations. vector supports all iterator ops: ++, -- (both prefix and postfix), =, ==, !=, *, - problem with going other directions: p = r.end(); p != r.begin(); p-- - doesn't work! r.end() is not an element, r.begin is, we're off - there are also reverse iterators vector::reverse_iterator rp; for (rp = vints.rbegin(); rp != vints.rend(); rp++) { } Container Classes -------------------- - sequential and associative, start with sequential - basic container classes - vector (dynamic array) - list (doubly linked list) - dqueue (super queue can add/remove on either end) - some member functions - size - begin - end - push_back(el) - push_front(el) - insert(iterator, el) - erase(iterator) erases AT location iterator - clear - front - ==, != - differences are mostly stylistic, efficiency - vector and dqueue can be randomly accessed via [] - list and dqueue can push new data in the front - built on top of these basic containers some more - stack - queue - priority_queue Associative Containers ------------------------ - use notion of key/value relationships so we can search by content of the data structur, instead of location like with sequential - set - insert/erase for adding deleting - keeps elements ordered, only one of each el - give the actual element to erase instead of iterator mys.insert('T'); mys.insert('R'); mys.erase('T'); - also now allows searching - mys.find('R') returns an iterator pointing at element 'R', end() is 'R' is not in the set - map - works on 'pair' element - pair another STL built in type - pair foo - foo.first = "hello"; - foo.second = 2; - map works on pairs map mymap; pair mypair; pair tmp; mypair.first = "dan"; mypair.second = 25; mymap.insert(mypair); tmp = mymap.find("dan"); cout << tmp.first << " " << tmp.second << endl; mymap.erase("dan"); - VERY useful data structure. Often times problems have key/value data characteristics -------------------- Project 5 ----------------- - TREE TREE TREE! - hows it going?