Presentation is loading. Please wait.

Presentation is loading. Please wait.

Containers and the Standard Template Library (STL)

Similar presentations


Presentation on theme: "Containers and the Standard Template Library (STL)"— Presentation transcript:

1 Containers and the Standard Template Library (STL)
Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Containers

2 From previous topic class BinaryTree { public: TreeNode *AddNode(const string &word); int getTotalNodes(); void PrintTree(ostream &output); BinaryTree(); //Default constructor ~BinaryTree(); //Destructor private: static int totalNodes; void PrintTree(TreeNode *subtree, ostream &output); TreeNode *AddNode(TreeNode *subtree, const string &word); TreeNode *root; }; // class BinaryTree There is something else unsatisfying about this implementation. What is it? CS-2303, A-Term 2012 Containers

3 From previous topic class BinaryTree { public: TreeNode *AddNode(const string &word); int getTotalNodes(); void PrintTree(ostream &output); BinaryTree(); //Default constructor ~BinaryTree(); //Destructor private: static int totalNodes; void PrintTree(TreeNode *subtree, ostream &output); TreeNode *AddNode(TreeNode *subtree, const string &word); TreeNode *root; }; // class BinaryTree The class TreeNode is also problem specific — it has a string and a count CS-2303, A-Term 2012 Containers

4 Consequence It would be a fair amount of work to adapt this binary tree implementation to some other purpose E.g., managing parse trees in a language translator Managing employee records Keeping track of baseball statistics ... It would be nice to have a binary tree class that would work for a wide variety of payload types CS-2303, A-Term 2012 Containers

5 Templates to the rescue!
template <class T> class TreeNode { public: friend class BinaryTree<T> friend class iterator<T> TreeNode(T data) : payload(data) {} private: T payload; TreeNode<T> *left; TreeNode<T> *right; } // template <class T> // class TreeNode template <class T> class BinaryTree { public: void insert(T item); TreeNode<T> *find(T item); … // iterator methods? private: TreeNode<T> * root; void insert(T item, TreeNode<T> *subtree); TreeNode<T> *find(T item, TreeNode<T> *subtree); } // template <class T> // class BinaryTree CS-2303, A-Term 2012 Containers

6 See Absolute C++ §17.4 & Figs 17.36 – 17.38
Result You can concentrate on building the binary tree Without worrying much about what it contains You can hand off the task of comparing nodes to the overloaded operators of <class T> You can take advantage of decades of knowledge and expertise accumulated about binary trees … and other forms of search trees See Absolute C++ §17.4 & Figs – 17.38 CS-2303, A-Term 2012 Containers

7 The Standard Template Library (STL)
A large collection of template container classes Also a bunch of other stuff Part of the C++ standard Embodies the collective wisdom and experience of the contributors CS-2303, A-Term 2012 Containers

8 Containers in the STL Vectors vector<bool>
Generalization of arrays Range checking Resizable All common C operators are overloaded to apply to vectors ++, --, [], ->, *, etc. Comprehensive set of iterators vector<bool> A highly optimized specialization of one-bit per element CS-2303, A-Term 2012 Containers

9 Containers in the STL (continued)
List Doubly-linked Stack Deque i.e., a double ended queue Queue All of the usual list operations, constrained to circumstances of the container Iterators specifically adapted to these containers CS-2303, A-Term 2012 Containers

10 Containers in the STL (continued)
basic_string The underlying class for string typedef basic_string<char> string Comparisons, assignments, iterators, etc. Substring, searching, pattern-matching String stream I.e., using << and >> to insert to or extract from strings instead of streams International character strings wchar, unicode, etc. CS-2303, A-Term 2012 Containers

11 Containers in the STL (continued)
map Associative array — search by value set Sets of objects Usual set operators for union, intersection, etc. bitset Array of booleans CS-2303, A-Term 2012 Containers

12 Other features in STL General utilities Iterators General algorithms
Date & time, memory allocators, etc. Iterators Lots of tools General algorithms Mostly for sets, partitions, sequences, binary searches, swapping, sorting, etc. Exception handling exception class, assertions, etc. Localization tools Representations of dates, currencies, numbers, etc. Numerics Numeric operations, complex, math functions, random numbers, etc. Input-output Streams, files, manipulators, etc. CS-2303, A-Term 2012 Containers

13 Questions? CS-2303, A-Term 2012 Containers


Download ppt "Containers and the Standard Template Library (STL)"

Similar presentations


Ads by Google