Weitere ähnliche Inhalte
Ähnlich wie 15. STL - Data Structures using C++ by Varsha Patil (20)
Kürzlich hochgeladen (20)
15. STL - Data Structures using C++ by Varsha Patil
- 2. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
2
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
- 3. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
3
The STL is a part of the standard C++ class library
and can be used as the standard approach for
storing and processing data
- 4. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
4
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
- 5. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
5
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
- 6. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
6
ADT stack(element)
Declare create() → stack
pop(push(e,S)) = S
getTop(stack) → element
is_empty(stack) → Boolean;
is_empty(create) = true
pop(stack) → stack
for all S Î stack, e Î element,
is_empty(push(e, S)) = false
pop(create()) = error
push(element, stack) → stack
Stack Abstract Data Type
- 7. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
7
A Survey of Programming
Object-oriented Programming
Techniques
- 8. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
8
Unstructured programming
Procedural programming
Modular programming
Object-oriented programming
A Survey of Programming
- 9. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
9
Unstructured programming: the main program
directly operates on global data
Unstructured programming
Program
PROGRAM
MAIN PROGRAM DATA
- 10. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
10
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
- 11. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
11
Execution of procedures: after processing,
flow of controls proceed where the call was made
Procedural Programming
Main program Procedure
- 12. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
12
Procedural programming: the main program coordinates calls to
procedures and hands over appropriate data as parameters
Procedural Programming
Program
PROGRAM
MAIN PROGRAM
DATA
PROCEDURE1 PROCEDUR
E2
PROCEDUR
E3
- 13. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
13
Modular
programming
Program
Main program
data
Module1
data+da
ta1
Module2
data+dat2
Procedure1 Procedure2 Procedure3
- 14. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
14
Object-oriented programming: objects of the program interact by
sending messages to each other
Object-oriented Programming
Program
Object1
data
Object4
data
Object
3 data
Object2
data
- 15. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
15
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
- 16. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
16
An object-oriented language is the one that supports
objects, and programs divided into objects,
contains objects belonging to a class,
supports inheritance
- 17. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
17
Objects
Classes
Data abstraction and encapsulation
Inheritance
Reusability
Polymerisation
Dynamic binding
Message passing
Basic Concepts of Object-oriented
Programming
- 18. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
18
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
- 19. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
19
Object-oriented programming encapsulates data
(attributes) and functions (behaviour) into package called
as classes
A class is a user-defined data type
Classes
- 20. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
20
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
- 21. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
21
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
- 22. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
22
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
- 23. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
23
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
- 24. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
24
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
- 25. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
25
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
- 26. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
26
A class template is a generic class declaration that allows the
user to provide the data structure through parameters that the
compiler resolved
- 28. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
28
Abstract representation of linked list
Linked list with header pointer
- 29. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
29
The C++ STL is a collection of
Containers
Iterators
Algorithms
- 30. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
30
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
- 31. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
31
Container 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
- 32. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
32
Containers are categorized into two types:
Sequence containers
Associative containers
- 33. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
33
The sequence containers are as follows:
Vectors
Lists
Deques
The containers that are derived from sequence
containers are as follows:
Stacks
Queues
Priority queues
Sequence containers
- 36. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
36
List of containers and their
characteristics
- 37. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
37
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
- 38. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
38
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
- 39. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
39
Suppose we create an array of type int storing
marks of the student
Then, int marks [6] = {73, 44, 42, 51, 59, 50}
- 40. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
40
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
- 41. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
41
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 arra
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
- 42. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
42
Iterator—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