Containers and the Standard Template Library (STL)

Slides:



Advertisements
Similar presentations
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Advertisements

Binary TreesCS-2303, C-Term Binary Trees (and Big “O” notation) CS-2303 System Programming Concepts (Slides include materials from The C Programming.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Programming Assignment #4 Binary Trees
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
The Design and Analysis of Algorithms
CS-2303 System Programming Concepts
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Data Structures Using C++ 2E
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Exam Review 2 Chapter 5 – 9 CSC212 FG CS Dept, CCNY.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
CS32 Discussion Section 1B Week 10 TA: Hao Yu (Cody)
CSCE 210 Data Structures and Algorithms
Introduction to Computers Computer Generations
Week 4 - Friday CS221.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
The Design and Analysis of Algorithms
Standard Template Library (STL)
Basic Data Structures.
Programming Assignment #4 Binary Trees in C++
Introduction to the Standard Template Library
Makefiles and Notes on Programming Assignment PA2
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
“Under the Hood” of Polymorphism
Standard Template Library Model
Classes, Constructors, etc., in C++
Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Miscellaneous C++ Topics
Structures, Unions, and Typedefs
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Bit Fields & Bitwise Operations
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Symbolic Constants in C
Design Patterns Difficult to describe abstractly Elements:
Programming Assignment #6
Scope Rules and Storage Types
Programming Assignment #5
Differences between Java and C
Your first C and C++ programs
Iterators Professor Hugh C. Lauer CS-2303, System Programming Concepts
Standard Version of Starting Out with C++, 4th Edition
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Digression on Loop Invariants
A Deeper Look at Classes
Standard Template Library
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
Standard Template Library
Introduction to Classes and Objects
Presentation transcript:

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

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

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

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

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

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.36 – 17.38 CS-2303, A-Term 2012 Containers

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

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

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

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

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

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

Questions? CS-2303, A-Term 2012 Containers