 2003 Prentice Hall, Inc. All rights reserved. 1 21.4.1 stack Adapter stack –Header –Insertions and deletions at one end –Last-in, first-out (LIFO) data.

Slides:



Advertisements
Similar presentations
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
More on the STL vector list stack queue priority_queue.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
 2006 Pearson Education, Inc. All rights reserved Standard Template Library (STL)
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to the Standard Template Library (STL) STL –Useful template-based components Containers:
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 21 - Standard Template Library (STL) Outline 21.1 Introduction to the Standard Template Library.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Data Structures Using C++ 2E
Containers Overview and Class Vector
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
1 CSC 262 Programming in C++ II Sykes Day 18.  We’ve repeatedly emphasized the importance of software reuse.  Recognizing that many data structures.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
 2006 Pearson Education, Inc. All rights reserved Standard Template Library (STL)
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
C++ STL CSCI 3110.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
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.
Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing.
 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.
 2003 Prentice Hall, Inc. All rights reserved Introduction to the Standard Template Library (STL) STL –Powerful, template-based components Containers:
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
Lecture 7 : Intro. to STL (Standard Template Library)
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Standard Template Library Lecture 9 March 22, 2005.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Standard Template Library Lecture 12 April 6, 2004.
Standard Template Library (STL) Outline 21.1 Introduction to the Standard Template Library (STL) Introduction to Containers Introduction.
C++ Review STL CONTAINERS.
STACK Data Structure
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
 2003 Prentice Hall, Inc. All rights reserved vector Sequence Container Declarations –std::vector v; type : int, float, etc. Iterators –std::vector.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 21 - Standard Template Library (STL) Outline 21.1 Introduction to the Standard Template Library.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Ders Notu 8 - Template İçerik 11.1 Giriş 11.2 Fonksiyon Template ları 11.3 Overloading Fonksiyon Templates.
Object-Oriented Programming (OOP) Lecture No. 42.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Object-Oriented Programming (OOP) Lecture No. 41
Chapter 22 - C++ Templates
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 21 - Standard Template Library (STL)
Chapter 21 - Standard Template Library (STL)
Chapter 21 - Standard Template Library (STL)
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 21 - Standard Template Library (STL)
Chapter 21 - Standard Template Library (STL)
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved stack Adapter stack –Header –Insertions and deletions at one end –Last-in, first-out (LIFO) data structure –Can use vector, list, or deque (default) –Declarations stack > myStack; stack > myOtherStack; stack anotherStack; // default deque vector, list –Implementation of stack (default deque ) –Does not change behavior, just performance ( deque and vector fastest)

 2003 Prentice Hall, Inc. All rights reserved. Outline 2 fig21_23.cpp (1 of 3) 1 // Fig : fig21_23.cpp 2 // Standard library adapter stack test program. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include // stack adapter definition 9 #include // vector class-template definition 10 #include // list class-template definition // popElements function-template prototype 13 template 14 void popElements( T &stackRef ); int main() 17 { 18 // stack with default underlying deque 19 std::stack intDequeStack; // stack with underlying vector 22 std::stack > intVectorStack; // stack with underlying list 25 std::stack > intListStack; 26 Create stacks with various implementations.

 2003 Prentice Hall, Inc. All rights reserved. Outline 3 fig21_23.cpp (2 of 3) 27 // push the values 0-9 onto each stack 28 for ( int i = 0; i < 10; ++i ) { 29 intDequeStack.push( i ); 30 intVectorStack.push( i ); 31 intListStack.push( i ); } // end for // display and remove elements from each stack 36 cout << "Popping from intDequeStack: "; 37 popElements( intDequeStack ); 38 cout << "\nPopping from intVectorStack: "; 39 popElements( intVectorStack ); 40 cout << "\nPopping from intListStack: "; 41 popElements( intListStack ); cout << endl; return 0; } // end main 48 Use member function push.

 2003 Prentice Hall, Inc. All rights reserved. Outline 4 fig21_23.cpp (3 of 3) fig21_23.cpp output (1 of 1) 49 // pop elements from stack object to which stackRef refers 50 template 51 void popElements( T &stackRef ) 52 { 53 while ( !stackRef.empty() ) { 54 cout << stackRef.top() << ' '; // view top element 55 stackRef.pop(); // remove top element } // end while } // end function popElements Popping from intDequeStack: Popping from intVectorStack: Popping from intListStack:

 2003 Prentice Hall, Inc. All rights reserved queue Adapter queue –Header –Insertions at back, deletions at front –First-in-first-out (FIFO) data structure –Implemented with list or deque (default) std::queue values; Functions –push( element ) Same as push_back, add to end –pop( element ) Implemented with pop_front, remove from front –empty() –size()

 2003 Prentice Hall, Inc. All rights reserved. Outline 6 fig21_24.cpp (1 of 2) 1 // Fig : fig21_24.cpp 2 // Standard library adapter queue test program. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include // queue adapter definition 9 10 int main() 11 { 12 std::queue values; // push elements onto queue values 15 values.push( 3.2 ); 16 values.push( 9.8 ); 17 values.push( 5.4 ); cout << "Popping from values: "; while ( !values.empty() ) { 22 cout << values.front() << ' '; // view front element 23 values.pop(); // remove element } // end while 26 Create queue, add values using push.

 2003 Prentice Hall, Inc. All rights reserved. Outline 7 fig21_24.cpp (2 of 2) fig21_24.cpp output (1 of 1) 27 cout << endl; return 0; } // end main Popping from values:

 2003 Prentice Hall, Inc. All rights reserved priority_queue Adapter priority_queue –Header –Insertions happen in sorted order, deletions from front –Implemented with vector (default) or deque –Highest priority element always removed first Heapsort algorithm puts largest elements at front less default, programmer can specify other comparator –Functions push(value), pop(value) top() –View top element size() empty()

 2003 Prentice Hall, Inc. All rights reserved. Outline 9 fig21_25.cpp (1 of 2) 1 // Fig : fig21_25.cpp 2 // Standard library adapter priority_queue test program. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include // priority_queue adapter definition 9 10 int main() 11 { 12 std::priority_queue priorities; // push elements onto priorities 15 priorities.push( 3.2 ); 16 priorities.push( 9.8 ); 17 priorities.push( 5.4 ); cout << "Popping from priorities: "; while ( !priorities.empty() ) { 22 cout << priorities.top() << ' '; // view top element 23 priorities.pop(); // remove top element } // end while 26 Create priority queue.Insert items using push. When using pop, highest priority (largest) items removed first.

 2003 Prentice Hall, Inc. All rights reserved. Outline 10 fig21_25.cpp (2 of 2) fig21_25.cpp output (1 of 1) 27 cout << endl; return 0; } // end main Popping from priorities:

 2003 Prentice Hall, Inc. All rights reserved Algorithms Before STL –Class libraries incompatible among vendors –Algorithms built into container classes STL separates containers and algorithms –Easier to add new algorithms –More efficient, avoids virtual function calls –

 2003 Prentice Hall, Inc. All rights reserved fill, fill_n, generate and generate_n Functions to change containers –fill(iterator1, iterator2, value); Sets range of elements to value –fill_n(iterator1, n, value); Sets n elements to value, starting at iterator1 –generate(iterator1, iterator2, function); Like fill, but calls function to set each value –generate(iterator1, quantity, function) Like fill_n, ""

 2003 Prentice Hall, Inc. All rights reserved. Outline 13 fig21_26.cpp (1 of 3) 1 // Fig : fig21_26.cpp 2 // Standard library algorithms fill, fill_n, generate 3 // and generate_n. 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 #include // algorithm definitions 10 #include // vector class-template definition char nextLetter(); // prototype int main() 15 { 16 std::vector chars( 10 ); 17 std::ostream_iterator output( cout, " " ); // fill chars with 5s 20 std::fill( chars.begin(), chars.end(), '5' ); cout << "Vector chars after filling with 5s:\n"; 23 std::copy( chars.begin(), chars.end(), output ); 24 Create vector of char s, to be used with various functions. Function fill.

 2003 Prentice Hall, Inc. All rights reserved. Outline 14 fig21_26.cpp (2 of 3) 25 // fill first five elements of chars with As 26 std::fill_n( chars.begin(), 5, 'A' ); cout << "\n\nVector chars after filling five elements" 29 << " with As:\n"; 30 std::copy( chars.begin(), chars.end(), output ); // generate values for all elements of chars with nextLetter 33 std::generate( chars.begin(), chars.end(), nextLetter ); cout << "\n\nVector chars after generating letters A-J:\n"; 36 std::copy( chars.begin(), chars.end(), output ); // generate values for first five elements of chars 39 // with nextLetter 40 std::generate_n( chars.begin(), 5, nextLetter ); cout << "\n\nVector chars after generating K-O for the" 43 << " first five elements:\n"; 44 std::copy( chars.begin(), chars.end(), output ); cout << endl; return 0; } // end main Functions generate and generate_n use function nextLetter.

 2003 Prentice Hall, Inc. All rights reserved. Outline 15 fig21_26.cpp (3 of 3) fig21_26.cpp output (1 of 1) // returns next letter in the alphabet (starts with A) 53 char nextLetter() 54 { 55 static char letter = 'A'; 56 return letter++; } // end function nextLetter Vector chars after filling with 5s: Vector chars after filling five elements with As: A A A A A Vector chars after generating letters A-J: A B C D E F G H I J Vector chars after generating K-O for the first five elements: K L M N O F G H I J