Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-1 Today’s Learning Objective  Standard Template Library.

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.
. 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.
Beginning C++ Through Game Programming, Second Edition
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
 2006 Pearson Education, Inc. All rights reserved Templates.
More on the STL vector list stack queue priority_queue.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter Section 4 Arrays and Classes (10.4)
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.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Data Structures Using C++ 2E
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
1 CSC 262 Programming in C++ II Sykes Day 18.  We’ve repeatedly emphasized the importance of software reuse.  Recognizing that many data structures.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Chapter 19 Standard Template Library Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
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)
C++ STL CSCI 3110.
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.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
Lecture 7 : Intro. to STL (Standard Template Library)
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 The Standard Template Library Drozdek Section 3.7.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Chapter 5 The MFC Collection Classes. How to store many data? 1. Use an Array 2. Use a Linked List value: 10 Node * p a a value: 20 Node * p value: 30.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Object-Oriented Programming (OOP) and C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
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
Standard Template Library
Chapter 4 Linked Structures.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Today’s Learning Objective
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Standard Template Library (STL)
Presentation transcript:

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Standard Template Library  Recall array and linkedlist structures  Standard Template Library (STL)  Includes libraries for all such data structures  Each data structure in STL contains  Containers: Like data structures  Iterators: Like pointers

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Containers  Container classes in STL  Each is template class with parameter for particular data type to be stored  e.g., Lists of int  Each has own iterator

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Different Containers  Sequential  1 st element, next element, … to last element  Examples: vector, linked list  Associative  Example: set and map

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Iterators  Generalization of a pointer  Typically even implemented with pointer  Designed to hide details of implementation  Provide uniform interface across different container classes  Each container class has "own" iterator type  Similar to how each data type has own pointer type

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Manipulating Iterators  overloaded operators:  ++, --, ==, !=  *  So if p is iterator variable, *p gives access to data pointed to by p  Vector container  has members begin() and end() c.begin();//Returns iterator for 1 st item in c c.end();//Returns "test" value for end

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Cycling with Iterators  Cycling ability (c is a container; p is a iterator): for (p=c.begin(); p!=c.end(); p++) { //process *p//*p is current data item }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Vector  Container:  vector container;  Iterator:  vector ::iterator p;  Difference between vector and array  Array size is fixed, while vector size is flexible

Example: Iterators Used with a Vector (1 of 2) #include using std::cout; using std::endl; using std::vector; int main( ) { vector container; for (int i = 1; i <= 4; i++) container.push_back(i); cout << "Here is what is in the container:\n"; vector ::iterator p; for (p = container.begin( ); p != container.end( ); p++) cout << *p << " "; cout << endl;

Example: Iterators Used with a Vector (2 of 2) cout << "Setting entries to 0:\n"; for (p = container.begin( ); p != container.end( ); p++) *p = 0; cout << "Container now contains:\n"; for (p = container.begin( ); p !=container.end( ); p++) cout << *p << " "; cout << endl; return 0; } Here is what is in the container: Setting entries to 0: Container now contains: 0 0

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Random Access of vector iterators

Copyright © 2006 Pearson Addison-Wesley. All rights reserved List  Container:  list container;  Iterator:  list ::iterator p;  Difference between vector and list  Vector allows random access while list do not allow

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 19.4 Two Kinds of Lists STL list is a double link list

Using the list Container(1 of 2) #include using std::cout; using std::endl; using std::list; int main( ) { list LO; for (int i = 1; i <= 3; i++) { LO.push_back(i); LO.push_front(i); } cout << "List contains:\n"; list ::iterator iter; for (iter = LO.begin(); iter != LO.end(); iter++) cout << *iter << " "; cout << endl;

Using the list Container(2 of 2) cout << "Setting all entries to 0:\n"; for (iter = LO.begin(); iter != LO.end(); iter++) *iter = 0; cout << "List now contains:\n"; for (iter = LO.begin(); iter != LO.end(); iter++) cout << *iter << " "; cout << endl; return 0; } List contains: Setting all entries to 0: List now contains: 0 0 0

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Set Container  Stores elements without repetition  1 st insertion places element in set  Each element value is its own key  Capabilities:  Add elements  Delete elements  Ask if element is in set

Set Container example (1) #include using std::set; using std::cout; using std::endl; using std::set ::iterator; int main() { set s; iterator p; s.insert('A'); s.insert('D'); s.insert('C'); s.insert('B'); Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Set Container example(2) cout << “The set contains:\n"; for (p=s.begin(); p!=s.end();p++) cout << *p << "\t"; cout << endl; s.erase('C'); cout << “The Set contains:\n"; for (p=s.begin(); p!=s.end();p++) { cout << *p << "\t"; } cout << endl; return 1; } Copyright © 2006 Pearson Addison-Wesley. All rights reserved The set contains: A B C D Removing C: The Set contains A B D

Copyright © 2006 Pearson Addison-Wesley. All rights reserved map Template Class  Stores pairs of data   Examples:   The first data (key type) is its key  Capabilities:  Add elements  Delete elements  Ask if element is in map