CS 403, Class 23Slide #1 CS 403 - Programming Languages Class 23 November 16, 2000.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
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.
Standard Containers: Vectors
What is generic programming? The essence of the generic programming approach is concept development: systematic classification of computing components.
Concept= a set of abstractions (e.g., types) (Generic Programming) Programming with Concepts defined by a set of requirements {vector, deque, list, set,
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
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,
. 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.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Writing Your Own STL Container Ray Lischner
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
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:
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Data Structures Using C++ 2E
Containers and Iterators CNS 3370 Copyright 2003, Fresh Sources, Inc.
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.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
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)
The C++ Standard Template Library. What is STL A subset of the standard C++ library –The string class is not part of it! 3 major components –Algorithms.
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
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.
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)
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.
1 Iterators Good reference site:
 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.
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.
Intro to the C++ STL Timmie Smith September 6, 2001.
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
1 CS Programming Languages Class 22 November 14, 2000.
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.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
C++ Review STL CONTAINERS.
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)
Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Introduction to Templates and Standard Template Library 1.
Object-Oriented Programming (OOP) Lecture No. 42.
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.
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.
Object-Oriented Programming (OOP) Lecture No. 41
CS212: Object Oriented Analysis and Design
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Programming with Concepts
Chapter 22: Standard Template Library (STL)
Miscellaneous Stuff Which there wasn’t enough time to cover
CS212: Object Oriented Analysis and Design
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
STL Библиотека стандартных шаблонов
Standard Template Library
Standard Template Library
Standard Template Library
Presentation transcript:

CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000

CS 403, Class 23Slide # 2 Today’s Agenda Augment Chapter 10 Quick (Re)Introduction to C++ Templates Overview of the Standard Template Library Example Code: deque & stack Announcement:. Programming Assignment, due next week

CS 403, Class 23Slide # 3 C++ Templates Parameterized data types Type-safe macros Come in two types Class Templates Function Templates

CS 403, Class 23Slide # 4 Class Templates Can specialize class (see listing #1) template class Stack… Stack Or any other type Stack

CS 403, Class 23Slide # 5 Class Template Member Functions Method body must be in.h file

CS 403, Class 23Slide # 6 Using a Class Template See listing #2 What is the output?

CS 403, Class 23Slide # 7 Function Templates Specialize a function to take polymorphic arguments Template T max(T a, T b) But be careful with pointer types… See listing #3 What does it print?

CS 403, Class 23Slide # 8 Template Function Specialization To fix “problem”, use type-specific version: template<> char* max(char* a, char* b) { return strcmp(a, b) > 0 ? a : b; }

CS 403, Class 23Slide # 9 Standard Template Library (STL) Generic Programming specify algorithms and data structures that work with any data type Core Components Containers Algorithms Iterators

CS 403, Class 23Slide # 10 STL: Containers Data structures that manage a set of memory locations Doesn’t contain many member functions Creating, copying, destroying, adding, removing No pointers to elements Algorithms do the “day-to-day” stuff

CS 403, Class 23Slide # 11 STL: Iterators Used to traverse elements of containers Uniform set/naming across containers Algorithms designed to work with a particular iterator category Random Access Bi-Directional Forward OutputInput

CS 403, Class 23Slide # 12 STL: Algorithms Decoupled from containers Parameterized by iterator types Algorithm categories: Non-mutating sequence operations Mutating sequence operations Searching and Sorting Set Operations Heap operations Numeric operations Miscellaneous

CS 403, Class 23Slide # 13 Orthogonal Component Structure So how does this all work together? vector v(3); v[0] = 7; v[1] = v[0] + 3; v[2] = v[0] + v[1]; reverse(v.begin(), v.end()); So what kind of components are v, v.begin(), and reverse? AlgorithmIteratorContainer

CS 403, Class 23Slide # 14 Example STL: deque Double-Ended QUEue Supports: Random access to elements Constant time insertion & removal of end Linear time insertion and removal of elements in the middle Constant time insertion & removal of beginning What role would the template parameter to deque fulfill?

CS 403, Class 23Slide # 15 Example STL: deque Use: deque Q; Q.push_back(3); Q.push_front(1); Q.insert(Q.begin() + 1, 2); Q[2] = 0; copy(Q.begin(), Q.end(), ostream iterator (cout, “ “)); What does this do?

CS 403, Class 23Slide # 16 Example STL: stack Adaptor that supports restricted subset of Container functionality Insertion, removal, and inspection of element at the top of the stack Does not allow iteration through its elements

CS 403, Class 23Slide # 17 Example STL: stack Example use: int main() { stack S; S.push(8); S.push(7); S.push(4); assert(S.size() == 3); assert(S.top() == 4); S.pop(); assert(S.top() == 7); S.pop(); assert(S.top() ==8); S.pop(); assert(S.empty()); }

CS 403, Class 23Slide # 18 Containers Sequences vector, deque list, slist, bit_vectorectordequeistslistbit_vector Associative Containers set, map, multiset, multimap, hash_set, hash_map, hash_multiset, hash_multimap, hashetmapmultisetmultimaphash_set hash_maphash_multisethash_multimaphash String package char_traits, basic_string, char_traitsbasic_string rope Container adaptors stack, queue, priority_queue, bitsettackqueuepriority_queuebitset

CS 403, Class 23Slide # 19 Algorithms Non-mutating algorithms for_each, find,, count, mismatch, equal, search Mutating algorithms copy, swap, transform, replace, fill, generate, remove, unique, reverse, rotate, random_shuffle, random_sample, partition, stable_partition, sorting, nth_element, binary search, merge, set operations heap operations, min and max, lexicographical_compare, permutations Generalized numeric algorithms iota, accumulate, inner_product, partial_sum, adjacent_difference, power

CS 403, Class 23Slide # 20 Iterators Iterator classes istream_iterator ostream_iterator front_insert_iterator back_insert_iterator insert_iterator reverse_iterator reverse_bidirectional_iterator raw_storage_iterator sequence_buffer