0% found this document useful (0 votes)
44 views

Chapter 1 Abstract Data Type and Introduction To STL (Part 1)

This document provides an introduction to abstract data types and the Standard Template Library (STL). It defines data structures as a way to store and organize data to be accessed and updated efficiently. Abstract data types (ADTs) define a type through a set of values and operations without specifying how those operations are implemented. The STL provides containers to store data, iterators to access elements in containers, and algorithms to perform operations on container elements. Containers, iterators, and algorithms work together through templates to provide a flexible and efficient way to work with data structures in C++.

Uploaded by

Lazy Studio
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Chapter 1 Abstract Data Type and Introduction To STL (Part 1)

This document provides an introduction to abstract data types and the Standard Template Library (STL). It defines data structures as a way to store and organize data to be accessed and updated efficiently. Abstract data types (ADTs) define a type through a set of values and operations without specifying how those operations are implemented. The STL provides containers to store data, iterators to access elements in containers, and algorithms to perform operations on container elements. Containers, iterators, and algorithms work together through templates to provide a flexible and efficient way to work with data structures in C++.

Uploaded by

Lazy Studio
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

C1

Abstract Data Type and


Introduction to STL
- Part 1
Learning Objectives

This module aims to provide students with understanding on:


 Data structures concepts
 Abstract Data Types
 Standard Template Library (STL)
 Vector and List
What is Data Structure?

 Programs are comprised of two things: data and algorithms.

 A data structure is a storage that is used to store and organize data. It is a way of
arranging data on a computer so that it can be accessed and updated efficiently. It is

 The choice of a particular data model depends on two considerations. First, it must
be rich enough in structure to mirror the actual relationships of the data in the real
world. On the other hands, the structure should be simple enough that one can
effectively process the data when necessary.

 Algorithms describe the way the data is to be transformed or manipulated.


What is Data Structure?

 The reason for learning about data structures is because adding structure to
our data can make the algorithms much simpler, easier to maintain, and often faster.

 Data structure also describes the relationship among the elements in addition to
memory storage. The data appearing in the data structure are processed by means of
certain operations. The four operations that play a major role are inserting, deleting,
traversing and searching.

 Other than that, sorting and merging are two additional operations used in special
situations.
Abstract Data Types

 In our program models, data structures are described by abstract data types (ADTs)

 We can create data structures or user-defined data types with the associated
operations to solve problems.

 Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by
a set of values and a set of operations.

 The definition of ADT only mentions what operations are to be performed but not
how these operations will be implemented.

 Examples: Array, Vector, List, Queue, Stack, Table, Tree and many more.
Abstract Data Types (cont.)
Abstract Data Types (cont.)

 Proper planning is essential to successful implementation of programs.

 Typical design methodologies focus on developing models of solutions before


implementing them.

 These models emphasize structure and function of the algorithms used.

 Initially our attention is on what needs to be done, not how it is done.

 So, we define program behavior in terms of operations to be performed on data.


Abstract Data Types : Example

 A stack is a last-in first-out (LIFO) linear structure where items can only be added and
removed from one end.

 Operations on this stack ADT might include:

 PUSH – add an item to the stack

 POP – remove the item at the top of the stack

 TOP – return the value of the item at the top of the stack

 EMPTY – determine if the stack is empty

 CREATE – create a new empty stack

 Notice these simply describe the things/what we can do, not how they are done.
These details will be reserved for implementation.
Standard Template Library

 Standard Template Library (STL) provides three generic


entities: containers, iterators, and algorithms, and a set of
containers
classes that overload the function operator called

STL Components
function objects.
iterators

 It also has a ready-made set of common classes for C++,


algorithms
such as container and associative arrays.

 These can be used with any built-in type and with any function objects

user-defined type that supports some elementary


operations.
Standard Template Library

 STL algorithms are independent of containers, which significantly reduces the


complexity of the library.

 The results of the STL are achieved through using templates.

 This allows the use of static binding polymorphism (as opposed to run-time or
dynamic binding polymorphism) which is frequently more efficient.
Standard Template Library : Container

 A container is a data structure that is typically designed to hold objects of the same
type and is a way stored data is organized in memory, e.g., an array of elements.

 Containers are implemented as template classes whose methods specify operations


on the data in the structures as well as the structures themselves.

 A number of these methods are common to all containers, while some are more
specific to the container they are defined in.

 The data stored in containers can be of any type and must supply some basic
methods and operations.

 This is especially necessary if pointers are involved.


Standard Template Library : Container
class MyClass{
struct Node{ int main(){
int data; Node *next; MyClass record;
}; record.createNode();

public: return 0;
MyClass(){ }
head = NULL;
}

Node* createNode(){
Node *n = new Node();
cout<<"Enter a number: ";
cin>>n->data;
n->next = NULL;

return n;
}
};
Standard Template Library : Iterators

 An iterator is an object that accesses the elements of a container.

 Iterators are a generalization of the concept of pointers,


they point to elements in a container, for example you can increment an iterator to p
oint to the next element in an array.

 These capabilities make iterators a major feature that allows the generality of the
STL.
Standard Template Library : Iterators

 The STL implements five types of iterators:

1. Input iterators : can read a sequence of values.

2. Output iterators : can write a sequence of values.

3. Forward iterators : can be read, written to, or moved


forward.

4. Bidirectional iterators : behaves like forward


iterators but can also move backwards.

5. Random iterators : can move freely in any direction


at one time.
Standard Template Library : Algorithms

 Algorithms in the STL are procedures that are applied to containers to process their
data, for example search for an element in an array, or sort an array.

 About 70 generic functions, known as algorithms, are provided in the STL

 These perform operations such as searching and sorting

 Each is implemented to require a certain level of iterator (and therefore will work on
any container that provides an interface by iterators)

 Algorithms are in addition to the methods provided by containers, but some


algorithms are implemented as member functions for efficiency
Standard Template Library : Relationship

• Sequence
Containers
Container • Derived Containers
• etc

• begin(), end() …
STL Iterator
• prev(), next() …

• sorting, searching..
Algorithm
• min, max..
End of Part 1

c l a s s . . !
See you in n e x t

You might also like