Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Lecture 20 “Standard” Template Library. What is the Standard Template Library? A collection of C++ classes (templates) containers vectors lists stacks.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Basic C++ Sequential Container Features
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Generic programming starts with algorithms. Lift Minimal requirements: works with maximal family of types Concrete algorithm: requires specific data type.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
Data Structures Using C++ 2E
Containers Overview and Class Vector
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
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++ STL CSCI 3110.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 16: Exceptions,
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Overview of C++ Templates
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
CS 403: Programming Languages Lecture 24 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
C++ Review STL CONTAINERS.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
CPS Searching, Maps, Tables (hashing) l Searching is a fundamentally important operation  We want to search quickly, very very quickly  Consider.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Software Design 2.1 STL concepts l Container: stores objects, supports iteration over the objects  Containers may be accessible in different orders 
CPS Searching, Maps, Tables l Searching is a fundamentally important operation ä We want to do these operations quickly ä Consider searching using.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Duke CPS Iterators: Patterns and STL l Access a container without knowing how it’s implemented ä libtapestry: first, isDone, next, current iterators.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
CPS Searching, Maps, Tables (hashing) l Searching is a fundamentally important operation  We want to search quickly, very very quickly  Consider.
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CS212: Object Oriented Analysis and Design
Iterators and STL Containers
Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Presentation transcript:

Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on a 21-element vector? Part of STL means crufty code (whose viewpoint?)  What about Java analog? l Differences in wordlines.cpp and tapwordlines.cpp  Map compared to tmap, what other kinds of maps?  Sets and vectors, which is easier to use? l Anything not clear in either program? Are these programs object-oriented?

Software Design 1.2 Standard Libraries l In C++ there is the Standard Library, formerly known as the Standard Template Library or STL  Emphasizes generic programming (using templates)  Write a sorting routine, the implementation depends on Elements being comparable Elements being assignable We should be able to write a routine not specific to int, string or any other type, but to a generic type that supports being comparable/assignable l In C++ a templated function/class is a code-factory, generates code specific to a type at compile time  Arguably hard to use and unsafe

Software Design 1.3 STL concepts l Container: stores objects, supports iteration over the objects  Containers may be accessible in different orders  Containers may support adding/removing elements  e.g., vector, map, set, deque, list, multiset, multimap l Iterator: interface between container and algorithm  Point to objects and move through a range of objects  Many kinds: input, forward, random access, bidirectional  Syntax is pointer like, analagous to (low-level) arrays l Algorithms  find, count, copy, sort, shuffle, reverse, …

Software Design 1.4 Iterator specifics l An iterator is dereferenceable, like a pointer  *it is the object an iterator points to l An iterator accesses half-open ranges, [first..last), it can have a value of last, but then not dereferenceable  Analagous to built-in arrays as we’ll see, one past end is ok l An iterator can be incremented to move through its range  Past-the-end iterators not incrementable vector v; for(int k=0; k < 23; k++) v.push_back(k); vector ::iterator it = v.begin(); while (it != v.end()) { cout << *v << endl; v++;}

Software Design 1.5 Iterator as Pattern l (GOF) Provides access to elements of aggregate object sequentially without exposing aggregate’s representation  Support multiple traversals  Supply uniform interface for different aggregates: this is polymorphic iteration (see C++ and Java) l Solution: tightly coupled classes for storing and iterating  Aggregate sometimes creates iterator (Factory pattern)  Iterator knows about aggregate, maintains state l Forces and consequences  Who controls iteration (internal iterator, apply in MultiSet)?  Who defines traversal method?  Robust in face of insertions and deletions?

Software Design 1.6 STL overview l STL implements generic programming in C++  Container classes, e.g., vector, stack, deque, set, map  Algorithms, e.g., search, sort, find, unique, match, …  Iterators: pointers to beginning and one past the end  Function objects: less, greater, comparators l Algorithms and containers decoupled, connected by iterators  Why is decoupling good?  Extensible: create new algorithms, new containers, new iterators, etc.  Syntax of iterators reflects array/pointer origins, an array can be used as an iterator

Software Design 1.7 STL examples: wordlines.cpp l How does an iterator work?  Start at beginning, iterate until end: use [first..last) interval  Pointer syntax to access element and make progress vector v; // push elements vector ::iterator first = v.begin(); vector ::iterator last = v.end(); while (first < last) { cout << *first << endl; ++first; }  Will the while loop work with an array/pointer? l In practice, iterators aren’t always explicitly defined, but passed as arguments to other STL functions

Software Design 1.8 Review: what’s a map, STL/Tapestry l Maps keys to values  Insert key/value pair  Extract value given a key  STL uses red-black tree, Tapestry uses bst or hashtable STL unofficially has a hash_map, see SGI website  Performance and other trade-offs? l In Tapestry, there’s an inheritance hierarchy of tmap, BSTMap, HMap  The hash-table requires map of string->value Makes programming simpler, too restrictive in practice  See tapwordlines.cpp