logistics
play

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

Logistics Project Standard Template Library 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


  1. Logistics • Project Standard Template Library – 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 • Tomorrow: STL 2 • Thursday: Exam 1 1

  2. A quick intro to Templates A quick intro to Templates template <class T> • To use this template: Datatype to be class Queue filled in later { // a queue of ints private: Queue<int> iqueue; T *q; int n; // a queue of doubles … Queue<double> dqueue; public: void enqueue (T i); // a queue of aClass objects T dequeue(); Queue<aClass> aqueue … } // a queue of pointers to aClass Queue<aClass *> aptrqueue 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 Sample code using STL #include <vector> • function objects (Functors) #include <algorithm> – Objects that overload operator(); #include <iostream> – Substitute for pointers to functions using namespace std; • adaptors vector<int> v; – adapt other components to special purposes. for (int i = 0; i < 25; i++) v.push_back(i); – Queues and stacks are adaptors random_shuffle(v.begin(), v.end()); • Allocators for (int j = 0; j < 25; j++) cout << v[j] << " "; – encapsulate a memory model. – decouple the algorithms from assumptions about a ... particular model. 2

  3. Simple Containers Vectors • vector • Will grow in size as you add stuff to them – Smart array • Add to the end of the vector (push_back) – Grows dynamically • Can insert (but expensive) – Random access (overrides []) • list • Remove from the end of the vector – Doubly-linked list (pop_back) – Sequential access • Can remove from middle (expensive) • deque – Double ended queue. • Random access (via operator[]) – Best of both vector and list Vector Lists #include <vector> • Can add to front or back #include <algorithm> #include <iostream> • Can insert (efficient) • Can remove from front, back, or middle using namespace std; (efficient) vector<int> v; for (int i = 0; i < 25; i++) v.push_back(i); • No operator[] random_shuffle(v.begin(), v.end()); for (int j = 0; j < 25; j++) cout << v[j] << " "; Lists Deque #include <list> • Can add to front or back #include <algorithm> #include <iostream> • Can insert (efficient) • Can remove from front, back, or middle using namespace std; (efficient) list<int> v; for (int i = 0; i < 25; i++) v.push_back(i); • Random access (operator []) for (int j = 0; j < 25; j++) { cout << v.front() << " "; v.pop_front(); } 3

  4. Deque Adaptor #include <deque> • Wrapper class #include <iostream> • Converts the interface of one object to using namespace std; another deque<int> v; • Hides the interface of the original object for (int i = 0; i < 25; i++) v.push_back(i); cout << v[13]; for (int j = 0; j < 25; j++) { cout << v.front() << " "; v.pop_front(); } Adaptor Queue // Accessors Queues and Stacks are Adaptors -- Take in Container Templates bool empty () const; size_type size () const; -- replaces it's own methods value_type& front (); const value_type& front () const; template <class T, class Container = value_type& back (); deque<T> > const value_type& back () const; class queue {...} void push (const value_type&); void pop (); Stack Questions? // Accessors bool empty () const; size_type size () const; value_type& top (); const value_type& top () const; void push (const value_type&); void pop (); 4

  5. Iterators Iterator Types • Iterators are used to step through elements • Some pointers are smarter than others in STL containers – forward_iterators – reverse_iterators – bidirectional_iterators • Written to emulate C/C++ pointers – const iterators – operator++ to iterate forward – Random access iterators – operator-- to iterate backwards – operator* to dereference. Iterator Types Getting Iterators – List // Iterators • All container methods that return a position in the container will return it as iterators iterator begin (); const_iterator begin () const; iterator end (); • Each container has predefined types for the const_iterator end () const; iterators it returns. reverse_iterator rbegin (); list<int> I; const_reverse_iterator rbegin () const; reverse_iterator rend (); list<int>::iterator it = I.begin(); const_reverse_iterator rend () const; Using Iterators Random Access Iterators • Allow for C-style pointer arithmetic’ list<int> I; list<int> I; list<int>::iterator it = begin(); list<int>::iterator it = begin(); it+= 4; while (it != I.end()) { cout << (*it); // Prints out 5th element of I. it++; cout << (*it); } 5

  6. Summary • Standard Template Library • Simple Containers • Iterators • Next Time: – More complex containers – Algorithms – Function Objects 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend