1 Read §6.1-6.3. 2 Templates Templates allow functions and classes to be _________________ so that the Templates provide a means to ________________—

Slides:



Advertisements
Similar presentations
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Advertisements

Chapter 6 Queues and Deques.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Chapter 7: User-Defined Functions II
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
The Standard Template Library provides the framework for building generic, highly reusable algorithms and data structures A reference implementation of.
Standard Containers: Vectors
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementations:
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
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.
1 Read § vector begin() end() sort(). 2 Templates Templates allow functions and classes to be parameterized so that the type of data being operated.
Templates and the STL.
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:
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Data Structures Using C++ 2E
Containers Overview and Class Vector
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementations:
1 Read § Templates Templates allow functions and classes to be parameterized so that the ______________ data being operated upon (or stored)
1 8. Run-Time Arrays — Intro. to Pointers For declarations like double doubleVar; char charVar = 'A'; int intVar = 1234; the compiler____________the object.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
1 8. Run-Time Arrays — Intro. to Pointers For declarations like double doubleVar; char charVar = 'A'; int intVar = 1234; the compiler constructs the object.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Templates&STL. Computer Programming II 2 Introduction They perform appropriate operations depending on the data type of the parameters passed to them.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
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.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
C++ Templates.
Introduction to Custom Templates
Dr. Bernard Chen Ph.D. University of Central Arkansas
Standard Template Library (STL)
Stacks Chapter 4.
CS212: Object Oriented Analysis and Design
Array Lists Chapter 6 Section 6.1 to 6.3
ADT Implementations: Templates and Standard Containers
Templates and Standard Containers
Java Programming Language
Lab4 problems More about templates Some STL
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

1 Read §

2 Templates Templates allow functions and classes to be _________________ so that the Templates provide a means to ________________— one template definition can be used to create The template mechanism is important and powerful. It is used throughout the Standard Template Library (STL) to achieve ________________.

3 Function Templates Example: Interchange the values of two int variables x and y. Instead of inline code: int temp = x; x = y; y = temp; write a function: /* Function to swap two integer variables. Receive: Integer variables first and second Pass back: first and second with values interchanged */ void Swap(______ & first, _______ & second) { _______ temp = first; first = second; second = temp; } Main reason for using functions:

4 Functions give a general solution to the interchange problem for int s because this Swap function can be used to exchange the values of any two integer variables: Swap(x, y);... Swap(w, z);... Swap(a, b); In contrast, inline code would have to be rewritten for each pair of integer variables to swap.

5 To interchange the values of two double variables: Can't use the preceding function; it swaps int s not double s. However, _______________ allows us to define multiple versions of the same function: /* Function to swap two double variables.... */ void Swap(________ & first, ________ & second) { ________ temp = first; first = second; second = temp; } The two different Swap functions are distinguished by the compiler according to each function's _________________ (name, number, type and order of parameters).

6 To interchange the values of two string variables: Again, overload function Swap() : /* Function to swap two string variables.... */ void Swap(________ & first, ________ & second) { ________ temp = first; first = second; second = temp; }

7 And so on... for other types of variables. Make a Swap library? OK for C++ predefined types, but can't use for user-defined types such as Time. We would have to overload Swap() for each user-defined type: /* Function to swap two Time variables.... */ void Swap(_______ & first, _______ & second) { _______ temp = first; first = second; second = temp; }

8 Observations: The _________ in each function is exactly the same. The only difference is in the _______________________ being exchanged. If we coul _______________________________________, we could write a general solution that could be used to exchange the values of any two variables.

9 Template Mechanism Declare a ____________________________________ and use it in the function instead of a specific type. This requires a ___________ ________________________________: /* Swap template for exchanging the values of any two objects of the same type Receive: _____________________________ first and second, two objects of same type Pass back: first and second with values swapped Assumes: Assignment (=) defined for type DataType */ ______________________________________________________ void Swap(____________ & first, ____________ & second) { ____________ temp; temp = first; first = second; second = temp; }

10 Notes: The word template is a C++ keyword specifying that what follows is a _______ for a function __________________________ “Normal” parameters (& arguments) appear within function parentheses; type parameters (& arguments for class templates) appear within template __________________________ Originally, the keyword __________was used instead of typename in a type-parameter list. ("class" as a synonym for "kind" or "category"— specifies "type/kind" of types.) Unlike other functions, a function template___________________ __________________. That is, we can't put prototype in a header file and definition in an implementation file; it all goes in the header file.

11 How is a Template Used? names DataType as a type parameter — value will determined by the ____________ from the _________________________ ________________________________________. Example: #include "Time.h" #include "Swap.h" //______ function template definition int main() { int i1, i2; double d1, d2; string s1, s2; Time t1, t2;... // Compiler generates ________________ // of Swap() with_____________________ _______________; // by ___________ _______________; // by ___________ }

12 General Form of Template (simplified) template FunctionDefinition or template FunctionDefinition where: TypeParam is a type-parameter (placeholder) naming the "generic" type of value(s) on which the function operates FunctionDefinition is the definition of the function, using type TypeParam.

13 Template Instantiation A function template is only a pattern that describes how individual functions can be built from given actual types. This process of constructing a function is called _______________. We instantiated Swap() four times — with types int, double, string, and Time. In each instantiation, the type parameter is said to be ___________ to the actual type passed A template thus serves as a pattern for the definition of an ________________________________________________. In and of itself, the template does nothing. For example, when the compiler encounters a template like Swap(), it stores the template but doesn't generate any machine instructions.

14 In and of itself, the template does nothing. When the compiler encounters a template like Swap(), it simply __________________________________ _______________________________________________. Later, when it encounters a call to Swap() like Swap(int1, int2); it generates an integer instance of Swap() : void Swap(int & first, int & second) { int temp = first; first = second; second = temp; } Algorithm for instantiation: (1) Search parameter list of function template for type parameters. (2) If one is found, determine type of corresponding argument. (3) Bind these types. For this instantiation to be possible, the compiler must "see" the actual definition of Swap(), and not just its prototype. This is why:  A function template ________________________________________ (prototype in a header file and definition in an implementation file.)

15 The previous example illustrates: Implicit instantiation: Compiler determines type by the parameter(s) in the function call. For efficiency (and often correctness), compilers frequently recommend Explicit instantiation: Provide a prototype of function to be later instantiated to the compiler. void Swap(int, int); void Swap(double, double); void Swap(string, string); void Swap(Time, Time);

16 Example /* Function template to find largest value of any type (for which < is defined) stored in an array. Receive: __________________________________________ array of elements of type ________________ numElements, number of values in array Return: Largest value in array */ { _____________ biggest = array[0]; for (int i = 1; i < numElements; i++) if (biggest < array[i]) biggest = array[i]; return biggest; }

17 #include "Max.h" #include using namespace std; int main () { double x[10] = {1.1, 4.4, 3.3, 5.5, 2.2}; cout << "Largest value in x is " << Max(x, 5); int num[20] = {2, 3, 4, 1}; cout <<"Largest value in num is "<< Max(num, 4); } Execution: Largest value in x is 5.5 Largest value in num is 4

18 When compiler encounters Max(x, 5), it: 1. Looks for__________________________________ — finds________________ 2. Finds type of______________________________________________________ 3. ____________________and__________________________________________ double Max(_________ array[], int numElements) { _________ biggest = array[0]; for (int i = 1; i < numElements; i++) if (biggest < array[i]) biggest = array[i]; return biggest; } Similarly, it generates an int version when Max(num, 4) is encountered.

19 Templates with multiple parameters template FunctionDefinition Each type parameter must appear at least once in the function's parameter list. Why? Compiler must be able to determine actual type corresponding to each type parameter from a function call. /* Function template to convert a value of any type to another type Receive: Type parameters Type1 and Type2 value1 of Type 1 Pass back: value2 of Type2 */ template void Convert(Type1 value1, Type2 & value2) { value2 = static_cast (value1); }

20 #include "Convert.h" #include using namespace std; int main() { char a = 'a'; int ia; Convert(a, ia); cout << a << " " << ia << endl; double x = 3.14; int ix; Convert(x, ix); cout << x << " " << ix << endl; } Sample run: a

21 The following version of function template Convert would not be legal: template Type2 Convert(Type1 value1) // Error--Type2 not in { // parameter list return static_cast (value1); } We could provide a dummy second parameter indicating the type of the return value: template Type2 Convert(Type1 value1, Type2 value2 = Type2(0)) { return static_cast (value1); } Function call: double x = 3.14; int ix = Convert(x, 0);

22 Class Templates Consider our Stack (and Queue) class: /* Stack.h contains the declaration of class Stack. Basic operations:... */ const int STACK_CAPACITY = 128; typedef int StackElement; class Stack { /***** Function Members *****/ public:... /***** Data Members *****/ private: StackElement myArray[STACK_CAPACITY ]; int myTop; };

23 What’s wrong with typedef ? To change the meaning of StackElement throughout the class, we need only change the type following the typedef. Problems: 1. Changes the header file  Any program/library that uses the class ________________ _______________. 2.A name declared using typedef can have ________________  If we need stacks with different elements types e.g., a Stack of ints and a Stack of strings, we must _____________________________________ ___________________________________.

24 Type-Independent Container Use a class template: the class is parameterized so that it receives the type of data stored in the class via a parameter (like function templates) /* StackT.h contains a template for class Stack Receives: Type parameter StackElement Basic operations... */ const int STACK_CAPACITY = 128; _____________________________________ class Stack { /***** Function Members *****/ public:... /***** Data Members *****/ private: _______________ myArray[STACK_CAPACITY]; int myTop; }; StackElement is a “blank” type (a type placeholder) to be filled in later.

25 General form of class template declaration template or template class SomeClass { //... members of SomeClass... }; More than one type parameter may be specified: template class SomeClass { //... members of SomeClass... };

26 Instantiating class templates To use a class template in a program/function/library: Instantiate it by using a declaration of the form ______________________________________ to _________________________________to the class template definition. Examples: Compiler will generate _________________________________ — two ____________ — one for int s and one for string s.

27 Rules for class templates For our Stack class: a. Prototypes of function members? No change — rules don't apply to them.

28 b. Definitions of function members? Rule 1: They must be defined as ________________________. _____________________________________________________ //... definition of Stack() _____________________________________________________ / /... definition of empty() template //... definition of push() template //... definition of display() template //... definition of top() template //... definition of pop()

29 Rule 2: The class name Stack preceding the scope operator ( :: ) is used as _______________________ and must therefore be ________________. template inline __________________________ Stack() { /*... body of Stack()... */ } template inline bool _______________________ empty() { /*... body of empty()... */ } template void Stack ::push(const StackElement & value) { /*... body of push()... */ } template void Stack ::display(ostream & out) { /*... body of display()... */ } template StackElement Stack ::top() { /*... body of top()... */ } template void Stack ::pop() { /*... body of pop()... */ }

30 Rule 3: These definitions must be placed __________________________: /* StackT.h provides a Stack template */ #ifndef STACKT #define STACKT... template class Stack {... }; // end of class declaration /***** Function Templates for Operations *****/ //--- Definition of Constructor template inline Stack ::Stack() { myTop = -1; }... #endif

31 c. Friend functions are also governed by the three rules. For example, to use operator<< instead of display() for output:  Prototype it within the class declaration as a friend: /* StackT.h provides a Stack template */... const int STACK_CAPACITY = 128; template class Stack { public: //--- Output operator -- documentation omitted here friend ostream & operator<<(ostream & out, ___________________________________);... }; // end of class Note:  Since Stack is being used as a type to declare the type of st, it must be parameterized.

32  And define it outside the class as a function template : // --- ostream output _________________________________________ ostream & operator<<(ostream & out, ___________________________________) { for (int pos = st.myTop; pos >= 0; pos--) out << st.myArray[pos] << endl; return out; }

33 Program to test this Stack template: #include using namespace std; #include "StackT.h" int main() { ___________________ // stack of ints ___________________ // stack of chars for (int i = 1; i <= 4; i++) intSt.push(i); cout << instSt << endl; for (char ch = 'A'; ch <= 'D'; ch++) charSt.push(ch); cout << charSt << endl; } Output: D C B A

34 Templates with ordinary parameters *** Templates may have more than one type parameter; they may also have __________________________________. /* StackT.h provides a Stack template. Receives: Type parameter StackElement _____________________________... */ #ifndef STACKT #define STACKT _____________________________________________________________ class Stack { public: //... Prototypes of member (and friend) functions... private: _________________________________________ int myTop; }; //... Definitions of member (and friend) functions... #endif

35 Program to test this modified Stack template:... int main() { ________________________ ________________________ // stack of 4 ints ________________________ ________________________ // stack of 3 chars for (int i = 1; i <= 4; i++) intSt.push(i); cout << instSt << endl; for (char ch = 'A'; ch <= 'D'; ch++) charSt.push(ch); cout << charSt << endl; } Output: *** Stack full -- can't add new value *** C B A

36 STL (Standard Template Library) A library of class and function templates based on work in generic programming done by Alex Stepanov and Meng Lee of the Hewlett Packard Laboratories in the early 1990s. It has three components: 1. ________________: Generic "off-the-shelf" class templates for storing collections of data 2. ______________: Generic "off-the-shelf" function templates for operating on containers 3. ______________: Generalized "smart" pointers that allow algorithms to operate on almost any container

37 STL's Containers In 1994, STL was adopted as a standard part of C++. There are ______ containers in STL: Kind of containerSTL Containers Sequential:___________________________________ Associative:___________________________________ Adapters:___________________________________ Non-STL: ___________________________________

38 vector Operations Constructors: vector v, // empty vector v1(100), // contains 100 elements of type T v2(100, val), // contains 100 copies of val v3(fptr,lptr); // contains copies of elements in // memory locations fptr up to lptr Copy constructor Destructor v.capacity() Number of elements v can contain without growing v.size() Number of elements v actually contains v.reserve(n) Increase capacity (but not size) to n v.empty() Check if v is empty v.push_back(val) Add val at end v.pop_back() Remove value at end v.front(), v.back(),Access first value, last value, v[i], v.at(i)i -th value without / with range checking ( at throws out-of-range exception - p. 272) Relational operatorsLexicographic order is used Assignment ( = )e.g., v1 = v2; v.swap(v1) Swap contents with those of vector v1

39 The other operations require knowledge of iterators. For example: v.begin() Returns iterator positioned at first element v.end() Returns iterator positioned immediately after last element v.insert(it, val) Inserts val at position specified by iterator it v.erase(it) Removes the element at position specified by iterator it Note: insert() moves all the elements from position it and following one position to the right to make room for the new one. erase() moves all the elements from position it and following one position to the left to close the gap. An iterator declaration for vector s has the form: vector ::iterator it; or using an iterator: Example: Function to display the values stored in a vector of double s: ostream & operator & v) { return out; }

40 A New (But Unnecessary) Revision of Our Stack Class Template Our class Stack still has one deficiency, namely, stacks can become full; they aren't dynamic in that it can grow when necessary. However, we could use vector as a container for the stack elements since it can grow automatically as needed, and the push_back() and pop_back() operations are perfect for stacks.... #include using namespace std; template class Stack { /***** Function Members *****/ public: Stack() {}; // let vector's constructor do the work bool empty() const; void push(const StackElement & value); void display(ostream & out) const; StackElement top() const; void pop();

41 /***** Data Members *****/ private: _____________________________________________________________ // don't need myTop -- back of vector is top of stack }; // end of class declaration //--- Definition of empty operation template inline bool Stack ::empty() const { return ___________________; } //--- Definition of push operation template void Stack ::push(const StackElement & value) { ______________________________; }

42 //--- Definition of display operation template void Stack ::display(ostream & out) const { for (int pos = myVector.size() - 1; pos >= 0; pos--) out << ___________________ << endl; /* or using a reverse iterator: for (vector ::reverse_iterator pos = myVector.rbegin(); pos != myVector.rend(); pos++) out << *pos << endl; */ } //--- Definition of top operation template StackElement Stack :: top() const { if (!empty()) return ___________________; //else cerr << "*** Stack is empty ***\n"; } }

43 //--- Definition of pop operation template void Stack :: pop() { if (!empty()) myVector.pop_back(); else cerr << "*** Stack is empty -- can't remove a value ***\n"; } Basically, all we have done is wrapped a vector inside a class template and let it do all the work. Our member functions are essentially just renamings of vector member functions. And there's really no need to do this, since STL has done it for us!

44 STL’s stack container STL includes a stack container. Actually, it is an ____________ — as indicated by the fact that its ______________________________________________ — which means basically that it is a class that acts as a _______________________________________________ so that it provides a ________________________________________________. A container adapter such as stack uses the members of the encapsulated container to implement what looks like a new container. For a stack, C may be any container that supports push_back() and pop_back() in a LIFO manner in particular C may be a _________, a _________, or a _________.

45 Basic operations: Constructor stack > st; creates an empty stack st of elements of type T ; it uses a container to store the elements. Note 1: The space between the two > s must be there to avoid confusing the compiler (else it treats it as >> ); for example, stack > s; not stack > s; Note 2: The default container is __________; that is, if " container" is omitted as in stack st; a ____________ will be used to store the stack elements. Thus stack st; is equivalent to ___________________________ ; Destructor Assignment, relational Operators size(), empty(), top(), push(), pop() Example: Converting to base two (where our whole discussion of stack began). See Fig. 6.8 on p. 300.

46 In queue, container type C may be list or deque. Why not vector ? ______________________________________ The default container is _____________. queue has same member functions and operations as stack except: push() adds item at back (our addQ() ) front() (instead of top() ) retrieves front item pop() removes front item (our removeQ() ) back() retrieves rear item STL's queue container

47 queue Example

48

49 Deques (Read pp ) As an ADT, a deque — an abbreviation for _______________________— is a sequential container that functions like a queue (or a stack) on both ends. It is an ordered collection of data items with the property that ____________________________________________________________. Basic operations are: Construct a deque (usually empty): Check if the deque is empty Push_front: Add an element at the front of the deque Push_back: Add an element at the back of the deque Front:Retrieve the element at the front of the deque Back: Retrieve the element at the back of the deque Pop_front:Remove the element at the front of the deque Pop_back:Remove the element at the back of the deque

50 STL's deque Class Template Has the same operations as vector except that there is no capacity() and no reserve() Has two new operations: d. push_front(value); Push copy of value at front of d d. pop_front(value); Remove value at the front of d Like STL's vector, it has several operations that are not defined for deque as an ADT: [] insert and delete at arbitrary points in the list, same kind of iterators. But insertion and deletion are not efficient and, in fact, take longer than for vector s.

51 vector vs. deque Capacity of a vector must be increased  it must copy the objects from the old vector to the new vector  it must destroy each object in the old vector  a lot of overhead! With deque this copying, creating, and destroying is avoided. Once an object is constructed, it can _______________________ ________________________________________ (if insertions and deletions take place at the ends of the deque ). Unlike vector s, a deque isn't stored in a single varying-sized block of memory, but rather in a collection of fixed-size blocks (typically, 4K bytes). One of its data members is essentially an array map whose elements point to the locations of these blocks.

52 For example, if each block consisted of only five memory locations, we might picture a deque containing 666, 777, 888, 999, 4, 3, 2, 1, 6, 5 in this order,from front to back, as follows: When a data block gets full, a new one is allocated and its address is added to map. When map gets full, a new one is allocated and the current values are copied into the middle of it. Inserts and deletes may involve cross-block element-shifting!

53 Bitsets and ValArrays (§6.7 & 6.8) The C++ standard includes bitset as a container, but it is not in STL. A bitset is an array whose elements are bits. It is much like an array whose elements are of type bool, but unlike arrays, it does provide operations for manipulating the bits stored in it. They provide an excellent data structure to use to implement sets. The standard C++ library also provides the valarray class template, which is designed to carry out (mathematical) vector operations very efficiently. That is, valarray s are (mathematical) vectors that have been highly optimized for numeric computations.

54 STL's algorithms (§7.5) Another major part of STL is its collection of more than 80 generic ________________. They are not member functions of STL's container classes and do not access containers directly. Rather they are stand-alone functions that operate on data by means of _____________. This makes it possible to work with regular C-style arrays as well as containers. We illustrate one of these algorithms here: sort. Sort 1: Using < #include using namespace std; // Add our Display() template for arrays int main() { int ints[] = {555, 33, 444, 22, 222, 777, 1, 66}; // must supply start and "past-the-end" pointers ______________________________ cout << "Sorted list of integers:\n"; Display(Ints, 8);

55 double dubs[] = {55.5, 3.3, 44.4, 2.2, 22.2, 77.7, 0.1}; ______________________________ cout << "\nSorted list of doubles:\n"; Display(Dubs, 7); string strs[] = {"good","morning","cpsc","186","class"}; ______________________________ cout << "\nSorted list of strings:\n"; Display(strs, 5); } Output: Sorted list of integers: Sorted list of doubles: Sorted list of strings: 186 class cpsc good morning

56 Sort 2: Supplying a "less-than" function to use in comparing elements #include using namespace std; bool IntLessThan(int a, int b) { return a > b; } bool DubLessThan(double a, double b) { return a > b; } bool StrLessThan(string a, string b) { return !(a < b) && !(a == b); }

57 int main() { int ints[] = {555, 33, 444, 22, 222, 777, 1, 66}; _____________________________________________ cout << "Sorted list of integers:\n"; Display(ints, 8); double dubs[] = {55.5,3.3,44.4,2.2,22.2,77.7,0.1}; _____________________________________________ cout << "Sorted list of doubles:\n"; Display(dubs, 7); string strs[] = {"good","morning","cpsc","186","class"}; _____________________________________________ cout << "Sorted list of strings:\n"; Display(strs, 5); } Output: Sorted list of integers: Sorted list of doubles: Sorted list of strings: morning good cpsc class 186

58 Sort 3: Sorting a vector of stacks using < (defined for stacks) #include using namespace std; #include "StackT.h" /* Add operator<() to our Stack class template as a member function with one Stack operand or as a friend function with two Stacks as operands. Or because of how we're defining < for Stacks here, st1 < st2 if top of st1 < top of st2 we can use the top() access function and make operator<() an ordinary function */ template bool operator & a, const Stack & b) { return a.top() < b.top();}

59 int main() { vector > st(4); // vector of 4 stacks of ints st[0].push(10); st[0].push(20); st[1].push(30); st[2].push(50); st[2].push(60); st[3].push(1); st[3].push(999); st[3].push(3); sort(st.begin(), st.end()); for (int i = 0; i < 4; i++) { cout << "Stack " << i << ":\n"; st[i].display(); cout << endl; } Output Stack 0: Stack 1: Stack 2: 30 Stack 3: 60 50