SlideShare une entreprise Scribd logo
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Day-2
19th February 2023
Data Structures- Using STL in C++
1
Instructor
Piyush Kumar Soni
Assistant Professor,
Information Technology Department
Workshop on
Data Structures &
Algorithms
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Contents
2
• Template Functions and Classes
• C++ Standard Template Library (STL)
• Iterators
• Algorithms
• Containers
• vectors
• list
• queue
• stack
• Functions
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Template Functions and Classes
3
• A template is a simple yet very powerful tool in C++. The idea is to pass data
type as a parameter so that we don’t need to write the same code for different
data types.
• Eg., we may need to sort() for different data types. Rather than writing and
maintaining multiple codes, we can write one sort() and pass data type as a
parameter.
• Keywords to support templates: ‘template’ and ‘typename’. The second
keyword can always be replaced by the keyword ‘class’.
• Templates are expanded at compile time.
• This is like macros. The difference is, that the compiler does type checking
before template expansion. The idea is simple, source code contains only
function/class, but compiled code may contain multiple copies of the same
function/class.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Template Functions
4
• Function Templates: We write a generic function that can be used for
different data types.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Template Classes
5
• Class Templates: Like function templates, class templates are useful when a
class defines something that is independent of the data type.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Standard Template Library (STL)
6
• The Standard Template Library (STL) is a set of C++ template classes to
provide common programming data structures and functions.
• It is a generalized library and so, its components are parameterized.
• The key benefits of the STL is that it provides a way to write generic,
reusable code that can be applied to different data types.
• Key components of the STL include
• Iterators
• Algorithms
• Containers
• Functions
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Iterators
7
• As the name suggests, iterators are used for working on a sequence of
values.
• Iterators are used to point at the memory addresses of STL containers.
• They reduce the complexity and execution time of the program.
• Operations:-
• begin() :- This function is used to return the beginning position of the
container.
• end() :- This function is used to return the after end position of the
container.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Iterators
8
• next() :- This function
returns the new iterator
that the iterator would
point after advancing
the positions mentioned
in its arguments.
• prev() :- This function
returns the new iterator
that the iterator would
point after decrementing
the positions mentioned
in its arguments.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Iterators
9
• inserter() :- This function is used to insert the elements at any position in the
container. It accepts 2 arguments, the container and iterator to position where
the elements have to be inserted.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Algorithms
10
• The header algorithm defines a collection of functions specially designed to
be used on a range of elements.
• They act on containers and provide means for various operations for the
contents of the containers.
• Examples:
• Sort
• Reverse
• Max
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Sort
11
• Sorting means arranging the data in a
particular fashion, which can be
increasing or decreasing.
• There is a built-in function in C++ STL
by the name of sort().
• This function internally uses IntroSort.
It is implemented using hybrid of
QuickSort, HeapSort and InsertionSort.
By default, it uses QuickSort but if
QuickSort is doing unfair partitioning
and taking more than N*logN time, it
switches to HeapSort and when the
array size becomes really small, it
switches to InsertionSort.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Reverse and Max
12
• reverse(first_iterator, last_iterator) – To reverse a vector. ( if ascending ->
descending OR if descending -> ascending)
• *max_element (first_iterator, last_iterator) – To find the maximum element
of a vector.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Containers
13
• A container is a holder object that stores a collection of other objects (its
elements). They are implemented as class templates, which allows great
flexibility in the types supported as elements.
• The container manages the storage space for its elements and provides
member functions to access them, either directly or through iterators
(reference objects with similar properties to pointers).
• Following is the list of containers available with STL:
• vector
• list
• deque
• arrays
• forward_list
• queue
• priority_queue
• stack
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Vector
14
• Vectors are the same as dynamic arrays with the ability to resize itself
automatically when an element is inserted or deleted, with their storage being
handled automatically by the container.
• Vector elements are placed in contiguous storage so that they can be accessed
and traversed using iterators.
• Funtions:
• begin() – Returns an iterator pointing to the first element in the vector
• end() – Returns an iterator pointing to the theoretical element that follows the last
element in the vector
• rbegin() – Returns a reverse iterator pointing to the last element in the vector (reverse
beginning). It moves from last to first element
• rend() – Returns a reverse iterator pointing to the theoretical element preceding the first
element in the vector (considered as reverse end)
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Vector
15
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Vector
16
• size() – Returns the
number of elements in the
vector.
• resize(n) – Resizes the
container so that it contains
‘n’ elements.
• empty() – Returns whether
the container is empty.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Stack
17
• Stacks are a type of container adaptors with LIFO(Last In First Out) type of
working, where a new element is added at one end (top) and an element is
removed from that end only.
• The functions associated with stack are:
• empty() – Returns whether the stack is empty
• size() – Returns the size of the stack
• top() – Returns a reference to the top most element of the stack
• push(g) – Adds the element ‘g’ at the top of the stack
• pop() – Deletes the most recent entered element of the stack
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Stack
18
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Queue
19
• Queues are a type of container adaptors that operate in a first in first out
(FIFO) type of arrangement. Elements are inserted at the back (end) and are
deleted from the front.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Problem-Statement-1
20
• Concatenation of Array:
• Given an integer array nums of length n, you want to create an array ans of
length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n
(0-indexed).
• Specifically, ans is the concatenation of two nums arrays.
• Return the array ans.
• Example:
Input: nums = [1,2,1]
Output: [1,2,1,1,2,1]
Explanation: The array ans is formed as follows:
- ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
- ans = [1,2,1,1,2,1]
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Solution
21
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Problem-Statement-2
22
• Kids With the Greatest Number of Candies:
• There are `n` kids with candies. You are given an integer array `candies`,
where each `candies[i]` represents the number of candies the `ith` kid has,
and an integer `extraCandies`, denoting the number of extra candies that you
have.
• Return a boolean array result of length n, where result[i] is true if, after
giving the ith kid all the extraCandies , they will have the greatest number of
candies among all the kids, or false otherwise.
• Note that multiple kids can have the greatest number of candies.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Solution
23
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Problem-Statement-3
24
• Reverse String:
• Solution:
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Problem-Statement-4
25
• Backspace String Compare:
• Given two strings s and t, return true if they are equal when both are typed
into empty text editors. '#' means a backspace character.
• Note that after backspacing an empty text, the text will continue empty.
• Example 1:
Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac".
• Example 2:
Input: s = "ab##", t = "c#d#"
Output: true
Explanation: Both s and t become "".
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Solution
26
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Problem-Statement-5
27
• Implement a stack using Queue:
• Solution:
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Solution
28
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Problem-Statement-6
29
• Number of Students Unable to Eat Lunch:
• The school cafeteria offers circular and square sandwiches at lunch break,
referred to by numbers `0` and `1` respectively. All students stand in a queue.
Each student either prefers square or circular sandwiches.
• The number of sandwiches in the cafeteria is equal to the number of students.
The sandwiches are placed in a stack. At each step:
• If the student at the front of the queue prefers the sandwich on the top of
the stack, they will take it and leave the queue.
• Otherwise, they will leave it and go to the queue's end.
• This continues until none of the queue students want to take the top
sandwich and are thus unable to eat.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Solution
30
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Thank You…!!!
31

Contenu connexe

Similaire à DSA-Day-2-PS.pptx

Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
Anonymous9etQKwW
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptx
sabithabanu83
 
Python - Lecture 12
Python - Lecture 12Python - Lecture 12
Python - Lecture 12
Ravi Kiran Khareedi
 
02 stackqueue
02 stackqueue02 stackqueue
02 stackqueue
Chandan Kumar
 
OOP
OOPOOP
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template Library
Anirudh Raja
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++
Khushal Mehta
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
 
CMSC 202 - Lec20 - Containers and Iterators(2).pptx
CMSC 202 - Lec20 - Containers and Iterators(2).pptxCMSC 202 - Lec20 - Containers and Iterators(2).pptx
CMSC 202 - Lec20 - Containers and Iterators(2).pptx
deathlyfire321
 
02 Stack
02 Stack02 Stack
Queues
QueuesQueues
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
GauravPatil318
 
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesData Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and Queues
Ferdin Joe John Joseph PhD
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
mayankKatiyar17
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
Michael Heron
 
Introduction data structure
Introduction data structureIntroduction data structure
Introduction data structure
Muhammad Ismail
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
kalai75
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
Senthil Kumar
 

Similaire à DSA-Day-2-PS.pptx (20)

Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptx
 
Python - Lecture 12
Python - Lecture 12Python - Lecture 12
Python - Lecture 12
 
02 stackqueue
02 stackqueue02 stackqueue
02 stackqueue
 
OOP
OOPOOP
OOP
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template Library
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Lecture 1 (bce-7)
Lecture   1 (bce-7)Lecture   1 (bce-7)
Lecture 1 (bce-7)
 
CMSC 202 - Lec20 - Containers and Iterators(2).pptx
CMSC 202 - Lec20 - Containers and Iterators(2).pptxCMSC 202 - Lec20 - Containers and Iterators(2).pptx
CMSC 202 - Lec20 - Containers and Iterators(2).pptx
 
02 Stack
02 Stack02 Stack
02 Stack
 
Queues
QueuesQueues
Queues
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and QueuesData Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 3 - Stacks and Queues
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
c++ Unit III - PPT.pptx
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
Introduction data structure
Introduction data structureIntroduction data structure
Introduction data structure
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 

Dernier

Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 

Dernier (20)

Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 

DSA-Day-2-PS.pptx

  • 1. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Day-2 19th February 2023 Data Structures- Using STL in C++ 1 Instructor Piyush Kumar Soni Assistant Professor, Information Technology Department Workshop on Data Structures & Algorithms
  • 2. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Contents 2 • Template Functions and Classes • C++ Standard Template Library (STL) • Iterators • Algorithms • Containers • vectors • list • queue • stack • Functions
  • 3. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Template Functions and Classes 3 • A template is a simple yet very powerful tool in C++. The idea is to pass data type as a parameter so that we don’t need to write the same code for different data types. • Eg., we may need to sort() for different data types. Rather than writing and maintaining multiple codes, we can write one sort() and pass data type as a parameter. • Keywords to support templates: ‘template’ and ‘typename’. The second keyword can always be replaced by the keyword ‘class’. • Templates are expanded at compile time. • This is like macros. The difference is, that the compiler does type checking before template expansion. The idea is simple, source code contains only function/class, but compiled code may contain multiple copies of the same function/class.
  • 4. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Template Functions 4 • Function Templates: We write a generic function that can be used for different data types.
  • 5. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Template Classes 5 • Class Templates: Like function templates, class templates are useful when a class defines something that is independent of the data type.
  • 6. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Standard Template Library (STL) 6 • The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions. • It is a generalized library and so, its components are parameterized. • The key benefits of the STL is that it provides a way to write generic, reusable code that can be applied to different data types. • Key components of the STL include • Iterators • Algorithms • Containers • Functions
  • 7. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Iterators 7 • As the name suggests, iterators are used for working on a sequence of values. • Iterators are used to point at the memory addresses of STL containers. • They reduce the complexity and execution time of the program. • Operations:- • begin() :- This function is used to return the beginning position of the container. • end() :- This function is used to return the after end position of the container.
  • 8. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Iterators 8 • next() :- This function returns the new iterator that the iterator would point after advancing the positions mentioned in its arguments. • prev() :- This function returns the new iterator that the iterator would point after decrementing the positions mentioned in its arguments.
  • 9. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Iterators 9 • inserter() :- This function is used to insert the elements at any position in the container. It accepts 2 arguments, the container and iterator to position where the elements have to be inserted.
  • 10. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Algorithms 10 • The header algorithm defines a collection of functions specially designed to be used on a range of elements. • They act on containers and provide means for various operations for the contents of the containers. • Examples: • Sort • Reverse • Max
  • 11. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Sort 11 • Sorting means arranging the data in a particular fashion, which can be increasing or decreasing. • There is a built-in function in C++ STL by the name of sort(). • This function internally uses IntroSort. It is implemented using hybrid of QuickSort, HeapSort and InsertionSort. By default, it uses QuickSort but if QuickSort is doing unfair partitioning and taking more than N*logN time, it switches to HeapSort and when the array size becomes really small, it switches to InsertionSort.
  • 12. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Reverse and Max 12 • reverse(first_iterator, last_iterator) – To reverse a vector. ( if ascending -> descending OR if descending -> ascending) • *max_element (first_iterator, last_iterator) – To find the maximum element of a vector.
  • 13. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Containers 13 • A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which allows great flexibility in the types supported as elements. • The container manages the storage space for its elements and provides member functions to access them, either directly or through iterators (reference objects with similar properties to pointers). • Following is the list of containers available with STL: • vector • list • deque • arrays • forward_list • queue • priority_queue • stack
  • 14. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Vector 14 • Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. • Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. • Funtions: • begin() – Returns an iterator pointing to the first element in the vector • end() – Returns an iterator pointing to the theoretical element that follows the last element in the vector • rbegin() – Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element • rend() – Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)
  • 15. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Vector 15
  • 16. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Vector 16 • size() – Returns the number of elements in the vector. • resize(n) – Resizes the container so that it contains ‘n’ elements. • empty() – Returns whether the container is empty.
  • 17. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Stack 17 • Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. • The functions associated with stack are: • empty() – Returns whether the stack is empty • size() – Returns the size of the stack • top() – Returns a reference to the top most element of the stack • push(g) – Adds the element ‘g’ at the top of the stack • pop() – Deletes the most recent entered element of the stack
  • 18. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Stack 18
  • 19. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Queue 19 • Queues are a type of container adaptors that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.
  • 20. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Problem-Statement-1 20 • Concatenation of Array: • Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed). • Specifically, ans is the concatenation of two nums arrays. • Return the array ans. • Example: Input: nums = [1,2,1] Output: [1,2,1,1,2,1] Explanation: The array ans is formed as follows: - ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]] - ans = [1,2,1,1,2,1]
  • 21. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Solution 21
  • 22. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Problem-Statement-2 22 • Kids With the Greatest Number of Candies: • There are `n` kids with candies. You are given an integer array `candies`, where each `candies[i]` represents the number of candies the `ith` kid has, and an integer `extraCandies`, denoting the number of extra candies that you have. • Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies , they will have the greatest number of candies among all the kids, or false otherwise. • Note that multiple kids can have the greatest number of candies.
  • 23. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Solution 23
  • 24. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Problem-Statement-3 24 • Reverse String: • Solution:
  • 25. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Problem-Statement-4 25 • Backspace String Compare: • Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character. • Note that after backspacing an empty text, the text will continue empty. • Example 1: Input: s = "ab#c", t = "ad#c" Output: true Explanation: Both s and t become "ac". • Example 2: Input: s = "ab##", t = "c#d#" Output: true Explanation: Both s and t become "".
  • 26. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Solution 26
  • 27. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Problem-Statement-5 27 • Implement a stack using Queue: • Solution:
  • 28. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Solution 28
  • 29. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Problem-Statement-6 29 • Number of Students Unable to Eat Lunch: • The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers `0` and `1` respectively. All students stand in a queue. Each student either prefers square or circular sandwiches. • The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step: • If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue. • Otherwise, they will leave it and go to the queue's end. • This continues until none of the queue students want to take the top sandwich and are thus unable to eat.
  • 30. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Solution 30
  • 31. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Thank You…!!! 31

Notes de l'éditeur

  1. Why study Data Structure and algorithm together?