![Page 1: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/1.jpg)
1Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
15. Standard Template Library (STL)
![Page 2: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/2.jpg)
2Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Objectives
Abstract data type (ADT) implementation in C++ and rationale for using them
How ADTs aid code reuse Five components of standard template
library (STL) and their power Simplify task of writing application
codes with the use of STL
![Page 3: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/3.jpg)
3Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Introduction The STL is a part of the standard C++
class library and can be used as the standard approach for storing and processing data
![Page 4: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/4.jpg)
4Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
A data type consists of a collection of values together with a set of basic operations defined on these values
A data type is called an ADT if a programmer can use it without having access to and also without knowing the details of how the values and operations are implemented
Abstract Data Type
![Page 5: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/5.jpg)
5Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
The term ADT describes a comprehensive collection of data values and operations
The term data structure refers to the study of data and how to represent data object within a program, that is, the implementation of structured relationship
Abstract Data Type and Data Structures
![Page 6: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/6.jpg)
6Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
ADT stack(element)Declare create() → stackpop(push(e,S)) = SgetTop(stack) → elementis_empty(stack) → Boolean;is_empty(create) = truepop(stack) → stackfor all S Î stack, e Î element,
is_empty(push(e, S)) = falsepop(create()) = errorpush(element, stack) → stack
Stack Abstract Data Type
![Page 7: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/7.jpg)
7Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
A Survey of Programming
Object-oriented Programming
Techniques
![Page 8: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/8.jpg)
8Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Unstructured programmingProcedural programmingModular programmingObject-oriented programming
A Survey of Programming
![Page 9: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/9.jpg)
9Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Unstructured programming: the main program directly operates on global data
Unstructured programming
ProgramPROGRAM
MAIN PROGRAM DATA
![Page 10: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/10.jpg)
10Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
With procedural programming, we are able to combine returning sequences of statements into one single place
A procedure call is used to invoke the procedure
Procedural Programming
![Page 11: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/11.jpg)
11Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Execution of procedures: after processing, flow of controls proceed where the call was made
Procedural Programming
Main program Procedure
![Page 12: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/12.jpg)
12Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Procedural programming: the main program coordinates calls to procedures and hands over appropriate data as parameters
Procedural Programming
ProgramPROGRAM
MAIN PROGRAM DATA
PROCEDURE1
PROCEDURE2
PROCEDURE3
![Page 13: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/13.jpg)
13Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Modular programming
Program
Main program data
Module1 data+data1
Module2 data+dat2
Procedure1
Procedure2
Procedure3
![Page 14: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/14.jpg)
14Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Object-oriented programming: objects of the program interact by sending messages to each other
Object-oriented Programming
ProgramObject1
dataObject4data
Object3 data
Object2 data
![Page 15: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/15.jpg)
15Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Object-oriented Programming
Object-oriented programming is a method of implementation in which
Objects are the fundamental building blocks Each object is an instance of some type (specification or class)
Objects can interact with each other Classes are related to each other by inheritance relationship
![Page 16: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/16.jpg)
16Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Object-oriented Programming
An object-oriented language is the one that supports objects, and programs divided into objects,
contains objects belonging to a class, supports inheritance
![Page 17: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/17.jpg)
17Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Objects Classes Data abstraction and encapsulation Inheritance Reusability Polymerisation Dynamic binding Message passing
Basic Concepts of Object-oriented Programming
![Page 18: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/18.jpg)
18Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Objects Objects are the basic runtime entities in an
object- oriented system Programming problem is analyzed in terms of
objects and the nature of communication between them
Each object contains data and code to manipulate the data
![Page 19: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/19.jpg)
19Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Object-oriented programming encapsulates data (attributes) and functions (behaviour) into package called as classes
A class is a user-defined data type
Classes
![Page 20: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/20.jpg)
20Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Data abstraction and encapsulation
Encapsulation : Combining a number of variables and functions into a single package
Abstraction : It refers to the act of representing essential features without including the details of implementation
Generally, data members are made private and are accessible to only class member functions
This insulation of data from direct access by the program is called data hiding or information hiding
![Page 21: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/21.jpg)
21Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Inheritance Inheritance is a process by which the
objects of one class inherit the properties of another class
Classes in C++ support the concept of hierarchical classification
![Page 22: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/22.jpg)
22Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Reusability The concept of inheritance providing
feature of reusability by additional features to the existing class without modifying the existing one leads to a new class
![Page 23: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/23.jpg)
23Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Polymorphism Polymorphism means the ability to take
more than one form Polymorphism is a mean by which we can
request an object to do something without knowing exactly what kind of object it is, and the object will figure out how to process the request appropriately
![Page 24: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/24.jpg)
24Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Binding refers to the linking of a procedure call to the code to be executed in response to the call
Dynamic binding means that the code associated with a given procedure call is not known until the time of call at runtime
This is associated with polymorphism and
inheritance
Dynamic binding
![Page 25: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/25.jpg)
25Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Message Passing An OOP consists of a set of objects that communicate
with each other
Message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired result
Message passing involves specifying the name of the object, the name of the function (message), and the information to be sent
![Page 26: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/26.jpg)
26Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
List Abstract Data Type
A class template is a generic class declaration that allows the user to provide the data structure through parameters that the compiler resolved
![Page 27: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/27.jpg)
27Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Linked list ADT structure
![Page 28: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/28.jpg)
28Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Abstract representation of linked list
Linked list with header pointer
![Page 29: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/29.jpg)
29Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Standard Template Library
The C++ STL is a collection of Containers Iterators Algorithms
![Page 30: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/30.jpg)
30Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Standard Template Library
The STL is a part of the standard C++ class library and can be used as a standard approach for storing and processing data
The task of writing complex application codes can be made easy with the use of STL
The STL allows programmer to use these classes and functions directly in programs to increase
productivity
![Page 31: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/31.jpg)
31Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
ContainersContainer class contains other objects. Container is a way to store data, whether the data consists of built-in types such as int and float, or of class objects
Container class Description Vector Array List Doubly linked list Slist Singly linked list Queue Queue structure, that is, FIFO structure Stack Stack structure, that is, LIFO structure Deque Combination of stack and queue, having facility for insertion and removal from both ends Set Set of unique elements Map Store key and data pair
![Page 32: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/32.jpg)
32Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Containers Categories
Containers are categorized into two types: Sequence containers Associative containers
![Page 33: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/33.jpg)
33Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
The sequence containers are as follows: VectorsListsDeques
The containers that are derived from sequence containers are as follows:
StacksQueuesPriority queues
Sequence containers
![Page 34: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/34.jpg)
34Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
List of functions for doubly ended queue
![Page 35: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/35.jpg)
35Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
List of a few functions for list
in STL
![Page 36: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/36.jpg)
36Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
List of containers and their characteristics
![Page 37: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/37.jpg)
37Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Associative Containers
An associative container is a collection of stored objects that allow fast retrieval using a key
In each container, the key must be unique There are four standard associative containers
classified into two classes: Sets (a) Set (b) Multiset Maps (a) Map (b) Multimap list
![Page 38: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/38.jpg)
38Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Algorithms The header <algorithm> defines a collection
of functions especially designed to be used on ranges of elements
These algorithms can be divided into six groups: Minimum and maximum algorithm Numeric algorithms Non-mutating sequence algorithms Sorting algorithm Set operations on sorted sequence Heap operations
![Page 39: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/39.jpg)
39Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Example Suppose we create an array of type int
storing marks of the student Then, int marks [6] = {73, 44, 42, 51, 59,
50}
![Page 40: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/40.jpg)
40Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
We can use STL sort() assort(marks, marks + 6)
Here, marks is the start and marks + 6 is the end addresses, respectively
Other example of sorting vector is as follows: vector<int> m;/having values 73, 44, 42, 51, 59, 50
sort(m.begin(), m.end() );// Output is 42, 44, 50, 51, 59, 73
sort(v.begin(), v.end(), greater<int>()); // Output is 73, 59, 51, 50, 44, 42
![Page 41: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/41.jpg)
41Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
replace() Replace value in range fill() Fill range with value remove() Remove value from range reverse() Reverse range sort() Sort elements in range partial_sort() Partially sort elements in range nth_element() Sort element in range binary_search() Test if value exists in sorted array merge() Merge sorted ranges min() Return the lesser of two arguments max() Return the greater of two arguments Min_element() Return the smallest element in range max_element() Return the largest element in range
![Page 42: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/42.jpg)
42Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
IteratorsIterator—Iterator is pointer like entity, which is used to access individual data items in a container, and it is used to store and retrieve objects in C++STL defines five different iterators
Forward Bidirectional Random access Input iterator Output iterator
![Page 43: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/43.jpg)
43Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
STL input iterator
![Page 44: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/44.jpg)
44Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
STL Output Iterator
![Page 45: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/45.jpg)
45Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
STL Forward Iterator
![Page 46: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/46.jpg)
46Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Bidirectional Iterator
![Page 47: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/47.jpg)
47Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Iterator Characteristics
![Page 48: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/48.jpg)
48Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Hierarchical relationship between
iterators
![Page 49: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/49.jpg)
49Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Iterator Operators
![Page 50: 15. STL - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.site/reader035/viewer/2022081604/5876d4b11a28ab1d238b5493/html5/thumbnails/50.jpg)
50Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
End of Chapter 1 5 …!