Project • Takeaway – submitted. • Next submission: Standard Template Library – Nim – Oct 26 th • Remember: okay to change framework – Make sure all games work with framework • Memory checks – Your program should do proper memory management! • Questions purify Project • Tool (make by Rational) that detects • I will be using purify in grading memory problems – Submission 3: Warnings! – Leaks – Final submission: points off. – Uninitialized reads – Delete Twice • Can only fix your own code! • To use – In Makefile: • CXX = purify g++ Announcement Before we begin • Final exam • Any questions? – Tuesday, November 18 th – 12:30pm – 2:30pm – Rm 70-3690
The Plan A quick intro to Templates template <class T> • Today: STL 1 Datatype to be class Queue filled in later • Tomorrow: STL 2 { private: • Thursday: Smart Pointers T *q; int n; … public: void enqueue (T i); T dequeue(); … } A quick intro to Templates The Standard Template Library • To use this template: • A general-purpose C++ library of algorithms and data structures // a queue of ints • Based on a concept known as generic Queue<int> iqueue; programming // a queue of doubles • Implemented by means of the C++ Queue<double> dqueue; template mechanism // a queue of aClass objects • Part of the standard ANSI C++ library Queue<aClass> aqueue • util package for C++ // a queue of pointers to aClass Queue<aClass *> aptrqueue STL Components STL Components • function objects (Functors) • containers – Objects that overload operator(); – classes that hold stuff – Substitute for pointers to functions • iterators • adaptors – Used to iterate through containers – adapt other components to special purposes. – Queues and stacks are adaptors – Generalization of C++ pointers • Allocators • generic algorithms – encapsulate a memory model. – Templated functions – decouple the algorithms from assumptions about a particular model.
Sample code using STL Simple Containers #include <vector> • vector #include <algorithm> – Smart array #include <iostream> – Grows dynamically using namespace std; – Random access (overrides []) vector<int> v; • list for (int i = 0; i < 25; i++) v.push_back(i); – Doubly-linked list random_shuffle(v.begin(), v.end()); – Sequential access • deque for (int j = 0; j < 25; j++) cout << v[j] << " "; – Double ended queue. ... – Best of both vector and list Vectors Vector #include <vector> • Will grow in size as you add stuff to them #include <algorithm> • Add to the end of the vector (push_back) #include <iostream> • Can insert (but expensive) using namespace std; • Remove from the end of the vector vector<int> v; (pop_back) for (int i = 0; i < 25; i++) v.push_back(i); • Can remove from middle (expensive) random_shuffle(v.begin(), v.end()); • Random access (via operator[]) for (int j = 0; j < 25; j++) cout << v[j] << " "; Lists Lists #include <list> • Can add to front or back #include <algorithm> • Can insert (efficient) #include <iostream> • Can remove from front, back, or middle using namespace std; (efficient) list<int> v; • No operator[] for (int i = 0; i < 25; i++) v.push_back(i); for (int j = 0; j < 25; j++) { cout << v.front() << " "; v.pop_front(); }
Deque Deque • Can add to front or back #include <deque> #include <iostream> • Can insert (efficient) using namespace std; • Can remove from front, back, or middle (efficient) deque<int> v; for (int i = 0; i < 25; i++) v.push_back(i); • Random access (operator []) cout << v[13]; for (int j = 0; j < 25; j++) { cout << v.front() << " "; v.pop_front(); } Adaptor Adaptor • Wrapper class Queues and Stacks are Adaptors • Converts the interface of one object to -- Take in Container Templates another -- replaces it's own methods • Hides the interface of the original object template <class T, class Container = deque<T> > class queue {...} Queue Stack // Accessors // Accessors bool empty () const; bool empty () const; size_type size () const; size_type size () const; value_type& front (); value_type& top (); const value_type& front () const; const value_type& top () const; value_type& back (); const value_type& back () const; void push (const value_type&); void push (const value_type&); void pop (); void pop ();
Questions? Iterators • Iterators are used to step through elements in STL containers • Written to emulate C/C++ pointers – operator++ to iterate forward – operator-- to iterate backwards – operator* to dereference. Iterator Types Iterator Types • All container methods that return a position • Some pointers are smarter than others in the container will return it as iterators – forward_iterators – reverse_iterators • Each container has predefined types for the – bidirectional_iterators iterators it returns. – const iterators list<int> I; – Random access iterators list<int>::iterator it = I.begin(); Getting Iterators – List Using Iterators // Iterators list<int> I; iterator begin (); const_iterator begin () const; list<int>::iterator it = begin(); iterator end (); while (it != I.end()) { const_iterator end () const; cout << (*it); reverse_iterator rbegin (); it++; const_reverse_iterator rbegin () const; } reverse_iterator rend (); const_reverse_iterator rend () const;
Random Access Iterators Summary • Standard Template Library • Allow for C-style pointer arithmetic’ • Simple Containers list<int> I; list<int>::iterator it = begin(); • Iterators it+= 4; • Next Time: // Prints out 5th element of I. – More complex containers cout << (*it); – Algorithms – Function Objects
Recommend
More recommend