15. STL - Data Structures using C++ by Varsha Patil
-
Upload
widespreadpromotion -
Category
Data & Analytics
-
view
741 -
download
0
Transcript of 15. STL - Data Structures using C++ by Varsha Patil
1Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
15. Standard Template Library (STL)
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
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
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
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
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
7Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
A Survey of Programming
Object-oriented Programming
Techniques
8Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Unstructured programmingProcedural programmingModular programmingObject-oriented programming
A Survey of Programming
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
27Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Linked list ADT structure
28Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Abstract representation of linked list
Linked list with header pointer
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
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
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
32Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Containers Categories
Containers are categorized into two types: Sequence containers Associative containers
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
34Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
List of functions for doubly ended queue
35Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
List of a few functions for list
in STL
36Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
List of containers and their characteristics
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
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
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}
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
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
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
43Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
STL input iterator
44Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
STL Output Iterator
45Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
STL Forward Iterator
46Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Bidirectional Iterator
47Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Iterator Characteristics
48Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Hierarchical relationship between
iterators
49Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Iterator Operators
50Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
End of Chapter 1 5 …!