Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.

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.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Beginning C++ Through Game Programming, Second Edition
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
Rossella Lau Lecture 11, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Rossella Lau Lecture 5, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque.
Rossella Lau Lecture 6, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 6: More on class construction UML and an.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
More on the STL vector list stack queue priority_queue.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.
Rossella Lau Lecture 2, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Rossella Lau Lecture 4, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 4: C++ and list  Usage of Vector and List  C++
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.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
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.
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
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
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)
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.
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.
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.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
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.
 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.
Lecture 7 : Intro. to STL (Standard Template Library)
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.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
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)
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Standard Template Library
CS212: Object Oriented Analysis and Design
Standard Template Library
C++ Standard Library.
Standard Template Library (STL)
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library
the Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
A dictionary lookup mechanism
Presentation transcript:

Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic components  Common functions of containers  Iterators and stream iterators  More on &  Simple and popular algorithms -- By Rossella Lau

Rossella Lau Lecture 12, DCO10105, Semester B, Introduction to the STL  The STL can be thought of as the extension of C++  SL is carried from the traditional C library  STL comes with the C++  There are three basic components: containers, algorithms, and iterators

Rossella Lau Lecture 12, DCO10105, Semester B, Components of the STL  The Standard Template Library (STL) includes  Containers: e.g., arrays, vector<>, pair<> store multiple occurrences of data in a particular data structure provide methods to process on its particular structure  Algorithms: e.g., find(), sort(), copy(), remove() provide non-member methods for generic structures use with iterators on containers  Iterators: e.g., i:0..n-1 as the subscript of an array

Rossella Lau Lecture 12, DCO10105, Semester B, General of containers  They are independent C++ template classes  Most are templates, only a few are inheritance, and no polymorphism  Consistent user-interface -- prototypes of member functions in different classes are as much the same as possible  All of the STL containers have the features of “true copy” or “deep copy”

Rossella Lau Lecture 12, DCO10105, Semester B, Types of Containers  Sequence Containers (sequential or fundamental containers); e.g. vector and list  Container Adapters; e.g., stack and queue;  Associated containers: e.g., set and map; they have special data structures  Others: e.g., array, string, pair<>

Rossella Lau Lecture 12, DCO10105, Semester B, The useful pair<>  pair is a template class in the STL. It allows easy definition of some objects which have two members. E.g., for getProduct() in Catalog.h : pair getProduct(string code) const; To locate the members, e.g for displayProductInfo() in gourmetCoffee.cpp : pair result = readProduct(); if ( result.first ) {… result.second …}

Rossella Lau Lecture 12, DCO10105, Semester B, Typical operations of containers (I)  Adding/inserting elements  push_back(), insert()  for list<>: push_front(); which is not supported by vector<>  Accessing / Mutating  front(), back()  for : [index], at(index)  Sizing  size(), empty()  for : capacity(), resize()  Removal  pop_back(), pop_front(), erase(), clear()  for : remove(), remove_if()

Rossella Lau Lecture 12, DCO10105, Semester B, Return as a reference  For mutators, the return type usually is a reference  The reference is an additional (another) name of the variable returned so that it can be modified  Without reference, only the value of the variable is returned

Rossella Lau Lecture 12, DCO10105, Semester B, Rename with &  Sometimes, some variables are too long to identify, e.g., Person person = persons[indexPerson] ; person.remove();  The codes above cannot properly remove the item in person  It CAN if the first line becomes: Person & person = persons[indexPerson] ;

Rossella Lau Lecture 12, DCO10105, Semester B, Typical operations of containers (II)  Construction  Comparison  Miscellaneous  E.g., sort()  For iteration (return type is an iterator)  begin(), end(), rbegin(), rend() Online reference:

Rossella Lau Lecture 12, DCO10105, Semester B, A note on compilation  The compiler does not incorporate particular error messages but usually lists what types (classes) of a template are supported and makes the error message terribly long; as a beginner coding with the STL, be patient and persistent, you will get through it!

Rossella Lau Lecture 12, DCO10105, Semester B,  An iterator is similar to the index for an array that leads a process to traverse from a position to another position of the container one by one  Each container provides its own iterators to allow generic algorithms to work on its elements; e.g., vector ::iterator  Typical iterators provided from each container:  begin().. end()  rbegin().. rend() // cannot be end().. begin(); While begin() refers to the first element of a container, end() refers to the position just after the last element -- this is also called a past-the-end iterator Iterator … begin() end()

Rossella Lau Lecture 12, DCO10105, Semester B, Iterator operations in C++  An iterator acts quite similar to a pointer in C++, it refers to a position of a container  Typical C++ iterator operations:  * de-reference the iterator as its pointing object   identify a member of an object pointed to by the iterator  ++ position the iterator to the next/previous element  == and != determine if two iterators are at the same position  = assign the position of an iterator to another iterator  E.g., in getProduct() of Catalog.h, for ( i = products.begin(); i != products.end() && (*i)->getCode() != code; i++);

Rossella Lau Lecture 12, DCO10105, Semester B, Typical loop with iterators  E.g., print the contents of a vector from the beginning:  E.g., print the contents of a vector from the end: vector studentID; vector ::iterator it = studentID.begin(); while ( it != studentID.end() ) { cout << *it++ << endl; vector studentID; vector ::iterator it = studentID.rbegin(); while ( it != studentID.rend() ) { cout << *it++ << endl;

Rossella Lau Lecture 12, DCO10105, Semester B, Some notes about Iterators  While begin() refers to the first element of a container, end() refers to the position just after the last element -- this is also called a past-the-end iterator  Not all containers allow the decrement operation "--" for iteration, to traverse a container in reverse order, the rbegin() and rend() should be used  Note that not all containers have reverse iterators  Since elements may not be positioned in the memory in a particular order, one should not determine if a container is not at the end by the checking of ( it < v.end() ) but (it ! = v.end())

Rossella Lau Lecture 12, DCO10105, Semester B, STL algorithms  General accessing of algorithms, such as searching and sorting, are abstracted as a single class to work on different containers and usually work with iterators  Other classes of algorithms are for special purposes; e.g., numerical algorithms  Example usages:  find() and accumulate() in Order.h in the application gourmetCoffee  However, operator==() and operator+=(double, OrderItem const &) must be defined in OrderItem.h

Rossella Lau Lecture 12, DCO10105, Semester B, Typical algorithms E.g., (Assume v is a vector or an array, it is an iterator)  it = find(v.begin(), v.end(), x); // returns an iterator if ( it != v.end() ) // found  sort(v.begin(), v.end()); //if operator<() is/can be defined sort(v.begin(), v.end(), lessThanFunctionID * );  When < cannot be overloaded for the class as a member function nor a non member function (since there should be at least one operand in the type of the class), e.g., elements in a container are pointers – Catalog.h

Rossella Lau Lecture 12, DCO10105, Semester B, Typical Algorithms with stream iterators  Stream iterators istream_iterator (anIstream) – beginning of an input istream_iterator () – end of the input ostream_iterator (anOstream,delimiter) E.g.; In sample programs tryCopy1.cpp and tryCopy2.cpp istream_iterator inFileIt(inFile); istream_iterator inFileEof; ostream_iterator outFileIt(cout, "\n");  Using copy() to output (without a loop!) copy (v.begin(), v.end(), outFileIt);  Using copy() to display an input file to the screen copy(inFileIt, inFileEof, outFileIt);

Rossella Lau Lecture 12, DCO10105, Semester B, Summary  The basic components of the STL are containers, iterators, and algorithms  Operator overload and Iterators link the containers and algorithms together  The ability for an algorithm to process on a container depends on what kinds of iterators it provides  Coding using the STL is similar to writing a unique language -- it tries to eliminate explicit loop as much as possible

Rossella Lau Lecture 12, DCO10105, Semester B, Reference  Malik: Appendix H  Some On line reference sites: END --