1 159.234 Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,

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.
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Data Structures Using C++ 2E
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Starting Out with C++, 3 rd Edition 1 Chapter 16 – Exceptions, Templates, and the Standard Template Library (STL) Exceptions are used to signal errors.
Beginning C++ Through Game Programming, Second Edition
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
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,
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.
Object Oriented Data Structures
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’
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
11 COS220 Concepts of PLs AUBG, COS dept Lecture 36 OOP The STL Standard Template Library Reference: MS Developer Studio, Visual C++, Lafore, Chap 15 STL,
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
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.
Array Size Macro approach 19 Macros Macros do not perform error checking. // macro approach #define macro_array_size(array) (sizeof(array)/sizeof(array[0]))
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 16: Exceptions,
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
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.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Intro to the C++ STL Timmie Smith September 6, 2001.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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)
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (this Friday). Today: –Templates. –STL –Smart Pointers.
Introduction to Templates and Standard Template Library 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
CS212: Object Oriented Analysis and Design
Standard Template Library
Standard Template Library (STL)
CS212: Object Oriented Analysis and Design
Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
An Introduction to STL.
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Presentation transcript:

Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252, Bjarne Stroustrup, The C++ Programming Language (3rd Ed.), A Tour of the Standard Library

2 Standard Template Library What is it? STL is a set of general-purpose template classes, built using the template mechanism. What are the main parts? Containers, Iterators, Algorithms.

3 Containers What are they? Containers are objects that hold other objects. There are different types of containers: sequence containers: vector, deque,list associative containers for allowing efficient retrieval of values based on keys: map, sets.

4 #include using namespace std; template // a generic func to print an array void printArray( T * a, int len ){ for(int j=0;j<len;j++) cout << a[j] << " "; cout << endl; } int main(){ //typedef long int MyType; typedef long long int MyType; const int Length = 10; MyType array[Length]; cout << "Size of MyType is: " << sizeof(MyType ) << endl; for( int i=0;i<Length;i++){ array[i] = i + 1; } printArray( array, Length ); MyType *ptr; MyType *start = array; MyType *end = array + Length; for( ptr=start;ptr!=end;ptr++){ *ptr *= -1; cout << ptr << endl; } printArray( array, Length ); return 0; } Preamble to iterators: Pointer arithmetic Example Output: Size of MyType is: xbffffc10 0xbffffc18 0xbffffc20 0xbffffc28 0xbffffc30 0xbffffc38 0xbffffc40 0xbffffc48 0xbffffc50 0xbffffc This code will work regardless of what MyType actually is. The operators + etc are all overloaded for pointers so that pointer arithmetic works as it should.

5 Vector class Vectors are dynamic arrays: arrays that can grow as needed. The template specification for the vector class is: template class vector{ //... }; When using the vector type the required header to be included is

6 Vector class Declaring/ defining a vector: vector iv; vector cv(5); vector cv(5, 'x'); vector iv2(iv);

7 Vector class Using a vector: // display original size of v cout<<"Size = "<<v.size()<<endl; /* put values onto end of a vector; the vector will grow as needed */ for(i=0; i<10; ++i) v.push_back(i); // change contents of a vector for(i=0; i<v.size(); ++i) v[i] = v[i] + v[i];

8 Vector class // access using subscripting for(i=0; i<10; ++i) cout <<v[i]<<" "; cout << endl; // access via iterator vector ::iterator p = v.begin(); while(p != v.end()) { cout << *p << " "; ++p; } Declaring an iterator: container_name :: iterator iterator_name

9 Iterator An Iterator is just a convenient pseudo-pointer that is set up as a type to use associated with each container class. Various operators will have been set up to use with them eg =, ==, != and += Standard idiomatic form: int array[10]; for(inti=0;I<10;I++){ } Becomes: vector v(10); vector ::iterator it; for(it=v.begin();it!=v.end();it++){ }

10 Iterators Iterators are objects similar to pointers. They are design to give us the ability to cycle through the content of a container. There are five iterator types: input output forward bidirectional random access

11 Iterators Container classes and algorithms dictate the category of iterator available or needed. vector containers allow random-access iterators lists do not; sorting requires a random-access iterators find requires an input iterator.

12 Iterators Iterators can be adapted to provide backward traversal. Example that uses a reverse iterator to traverse a sequence. template void print(ForwIter first, ForwIter last, const char* title){ cout << title << endl; while ( first != last) cout << *first++ << '\t'; cout << endl; }

13 Iterators int main(){ int data[3] = { 9, 10, 11}; vector d(data, data + 3); vector ::reverse_iterator p = d.rbegin(); print(p, d.rend(), "Reverse"); //... }

14 Other standard components STL relies upon several other standard components for support: allocators: to manage memory allocation for a container predicates: special functions returning true/false results comparison functions, etc.

15 Summary The standard template library (STL) is the C++ library that provides generic programming for many standard data structures and algorithms. Containers come in two major families: sequence (are ordered) and associative (have keys for looking up elements). Iterators can be thought of as an enhanced pointer type. The algorithms use iterators to access containers.

16 C++ Standard C++Standard: International standard for the C++ programming language approved! Morristown, New Jersey, USA, Friday, November 14, 1997 FOR IMMEDIATE RELEASE This week, technical experts representing eight countries and about 40 companies involved with software technologies met in Morristown, New Jersey and completed the content of an international standard for the C++ programming language.

17 Scope of the Standard: The C++ standard covers both the C++ language itself and its standard library. The standard library provides standard input/output, strings, containers (such as vectors, lists, and strings), non-numerical algorithms (such as sort, search, and merge), and support for numeric computation.

18 The ISO/ANSI Standard for C++ was unanimously approved by the C++ Standardization Committee on June 23, From: The current status (June’99) is that C++ has an International Standard and the committee are in the process of handling Defect Reports. We shall continue to do this until 2003 when ISO rules require us to revise the Standard. From: