data structures lecture-12 : stl azhar maqsood nust institute of information technology (niit)
TRANSCRIPT
![Page 1: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/1.jpg)
Data Structures
Lecture-12 : STL
Azhar MaqsoodNUST Institute of Information Technology (NIIT)
![Page 2: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/2.jpg)
Stack Specification
• Definitions: (provided by the user)– MAX_ITEMS: Max number of items that might be on
the stack
– ItemType: Data type of the items on the stack
• Operations– Boolean IsEmpty
– Boolean IsFull
– Push (ItemType newItem)
– Pop (ItemType& item)
![Page 3: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/3.jpg)
Stack Implementation
#include "ItemType.h"// Must be provided by the user of the class// Contains definitions for MAX_ITEMS and ItemType
class StackType { public: StackType(); void MakeEmpty();
bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&);private: int top; ItemType items[MAX_ITEMS];};
![Page 4: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/4.jpg)
Stack Implementation (cont.)
StackType::StackType(){ top = -1;}
void StackType::MakeEmpty() { top = -1;}
bool StackType::IsEmpty() const{ return (top == -1);}
![Page 5: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/5.jpg)
Stack Implementation (cont.)
bool StackType::IsFull() const{ return (top == MAX_ITEMS-1);}
void StackType::Push(ItemType newItem){ top++; items[top] = newItem;}
void StackType::Pop(ItemType& item){ item = items[top]; top--;}
![Page 6: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/6.jpg)
Overview of STL
• STL: Standard Template Library– A generic library that provides solutions to manage
collections of data with modern and efficient algorithms.
– The heart of the C++ standard library
• STL includes the following components:– Data Structures – (vector, list, set, …)
– Generic Algorithms – (for each DS: find, sort, …)
– Object Functions – (e.g. math function, logic function,…)
– Allocators
![Page 7: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/7.jpg)
Implementing stacks using templates
• Templates allow the compiler to generate multiple versions of a class type or a function by allowing parameterized types.
• It is similar to passing a parameter to a function (we pass a data type to a class !!)
![Page 8: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/8.jpg)
Class templates
• Syntax: template <class T>class myStack {...};
• T represents a type.• Both the declaration and the definition of a class
template are placed in the same file.
• To declare a stack of integers: myStack<int> numStack;
![Page 9: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/9.jpg)
Implementing stacks using templates
template<class ItemType>class StackType { public: StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&);
private: int top; ItemType items[MAX_ITEMS];};
(cont.)
![Page 10: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/10.jpg)
Example using templates
// Client codeStackType<int> myStack;StackType<float> yourStack;StackType<StrType> anotherStack; myStack.Push(35);yourStack.Push(584.39);
The compiler generates distinct class types and gives its own internal name to each of the types.
![Page 11: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/11.jpg)
Function templates
• The definitions of the member functions must be rewritten as function templates.
template<class ItemType>StackType<ItemType>::StackType(){ top = -1;}
template<class ItemType>void StackType<ItemType>::MakeEmpty(){ top = -1;}
![Page 12: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/12.jpg)
Function templates (cont.) template<class ItemType>bool StackType<ItemType>::IsEmpty() const{
return (top == -1);}
template<class ItemType>bool StackType<ItemType>::IsFull() const{
return (top == MAX_ITEMS-1);}
template<class ItemType>void StackType<ItemType>::Push(ItemType newItem){
top++; items[top] = newItem;}
![Page 13: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/13.jpg)
Function templates (cont.)
template<class ItemType>void StackType<ItemType>::Pop(ItemType& item){ item = items[top]; top--;}
![Page 14: Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649e405503460f94b30c6f/html5/thumbnails/14.jpg)
Comments using templates
• The template<class T> designation must precede the class method name in the source code for each template class method.
• The word class is required by the syntax of the language and does not mean that the actual parameter must be the name of a class.
• Passing a parameter to a template has an effect at compile time.