Announcement • Final exam Standard Template Library – Tuesday, May 20 th – 2:45 – 4:45pm – Rm 70-1620 Speaking of Exams Projects • There will be no exam this week Mandlebrot table on Web is now fixed. • Next exam: – May 1 st deliverable: – Topic list to come – File Image: OPEN TILL END OF TODAY – Fractal Image: Due May 2 nd • Questions? The Plan A quick intro to Templates template <class T> • Today: STL 1 Datatype to be class Queue filled in later • Wednesday: 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 for (int j = 0; j < 25; j++) cout << v[j] << " "; • deque – Double ended queue. ... – Best of both vector and list
Vectors Vector • Will grow in size as you add stuff to them #include <vector> #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 #include <deque> • Can add to front or back #include <iostream> • Can insert (efficient) using namespace std; • Can remove from front, back, or middle queue<int> v; (efficient) 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