SlideShare a Scribd company logo
1 of 36
StandardTemplate
Library
What is a
Standard
Library ?
In the C++ programming language, the Standard Library
is a collection of classes and functions.
The C++ Standard Library can be categorized into two
parts −
1. The Standard Function Library
2. The Object-Oriented Class Library
What are
Templates ?
• The simple idea is to pass data type as a
parameter so that we don’t need to write the
same code for different data types.
• C++ adds two new keywords to support
templates: ‘template’ and ‘typename’. The
second keyword can always be replaced by
keyword ‘class’.
How Templates work
• Templates are expanded
at compiler time.
• The difference is, compiler
does type checking before
template expansion.
• The source code contains
only function/class, but
compiled code may
contain multiple copies of
same function/class.
What is
Standard
Template
Library ?
The Standard Template Library (STL) is a
set of C++ template classes to provide
common programming data structures and
functions such as lists, stacks, arrays, etc.
A working knowledge of template
classes is a prerequisite for working with
STL.
Components of STL
STL Component Overview
Data Storage
Containers
• Hold Data
Iterators
• Access Data
Algorithms
• Manipulates Data
Data Access
Algorithms
Containers
• Containers or container classes store
objects and data.
• There are in total seven standard “first-
class” container classes and three
container adaptor classes and only
seven header files that provide access to
these containers or container adaptors.
3Types of Container
Sequence Container
Container Adapters
Associative Container
Sequence
Container
Implements data structures which can
be accessed in a sequential manner.
Vector List Deque
What is Vector
Class?
• Vector is a template class in STL (Standard Template
Library) of C++ programming language.
• These are sequence containers that store elements.
• They can automatically manage storage. It is efficient if you
add and delete data often.
Some Methods of Vector Class
#include <vector>
To import Vector Class
size( )
Number of
Elemets
push_back( )
Appends at end
pop_back( )
Erase last
Element
begin( )
Reference to
first element
end( )
Reference to
end of Vector
Example of Vector Class
Vector <int> v = { 12 , 7 , 9 ,
21 , 13 } ; 12 7 9 21 13
12 7 9 21
13
v.pop_bac
k() ;
12 7 9 21
13
…..
12 7 9 21 15
v.push_back(
15) ;
12 7 9 21 13
0 1 2 3 4
v.begin v[3 v.end(
What is a List?
• Sequence Containers - allow constant time insert and erase
operations.
• Store elements they contain in different and unrelated
storage locations.
• Lists - better in inserting, extracting and moving elements
in any position within the container for which an iterator
has already been obtained, and therefore also in
algorithms that make intensive use of these, like sorting
algorithms.
• Consume some extra memory to keep the linking
information associated to each element.
Some
Operations
of List Class
Operations Uses
sort Sort elements in a list
splice Transfer elements from 1 list to another
list
merge Merge sorted lists
unique Removes duplicate elements
swap Exchange the content of 1 list with that
of another list.
assign Assigns new contents to the list
remove Erase all instances of value
Example of List Class
list <int> li = { 12 , 7 , 9 , 21 ,
13 } ; 12 7 9 21 13
12 7 9 21
13
li.pop_bac
k() ;
12 7 9 21
13
…..
12 7 9 21 15
v.push_back(
15) ;
12 7 9 21
7 9 21 15
12 li.pop_fro
nt() ;
14 7 9 21 15
…..
v.push_front(
14) ;
Deque Class
• Sequence containers with the feature of expansion and
contraction on both the ends.
• Implemented as dynamic arrays.
• To use deque container in a program include statement:
#include <deque>
• Syntax: Deque declaration
deque <elementType> name(size);
Some
Operations of
Deque Class
Operation Description
insert Inserts an element.
maxsize Return the max no of elements.
pushfront Push elements into a deque from the front.
pushback Push elements into a deque from the back.
popfront Pop/remove elements from a deque from
the front.
popback Pop/remove elements from a deque from
the back.
clear Remove all the elements of the deque
container.
Container
Adaptors
Provide a different interface for
sequential containers.
Queue
Priority
Queue
Stack
Types of
Container
Adaptors
• Queue: A queue can store elements in the sequence
container list or deque data structure.
• Priority queue: The priority queue adapter class is a little
bit different in the sense that it enables the insertion of
elements in sorted order into the data structure and
removal of elements occurs from the front of the waiting
line.
• Stack: The class called stack is an implementation of the
stack data structure where insertion and retrieval of
elements occurs according to the FIFO manner
Associative
Containers
Implement sorted data structures
that can be quickly searched (O(log
n) complexity).
set multiset map multimap
Types of
Iterators
Map
Set
Multimap
Multiset
Iterators • What are iterators?
• How many types of iterators are provided by
C++ ?
• Can iterators be null in C++ ?
Iterators
17
17
23
12
17
4
array
size 4
Iterators are pointer like entries that are
used to access individual elements in the
container
Often, they are used to move
sequentially from element to element, a
process called iterating through
container.
Iterators
17
17
23
12
17
4
array
size 4
The Member function begin() and end()
return an iterator to the first and past
the last element of a container
17
v.begin
()
12
v.end()
Algorithms
• Collection of functions especially designed to be used
on ranges of elements.
• They act on containers and provide means for various
operations for the contents of the containers.
• Range - Sequence of objects that can be accessed
through iterators or pointers, such as an array or an
instance of some of the STL containers.
Some of the most used Algorithms
Sort
Algorithm
Reverse
Algorithm
Max element
Algorithm
Min element
Algorithm
Accumulate
Algorithm
Find
Algorithm
Count
Algorithm
Sort Algorithm
Sort algorithm - To sort a given
Vector
sort Prototype -
sort(first_iterator, last_iterator)
Example –
vector <int> V{ 10,20,40,15};
sort(V.begin(),V.end());
Reverse Algorithm
Reverse Algorithm – To reverse
a given vector
Reverse Prototype –
reverse(first_iterator,
last_iterator)
Example –
vector <int> V{ 12,8,9,15,7};
reverse(V.begin(),V.end());
Max element
Algorithm
Max element Algorithm – To find
the maximum element of a given
vector.
*max_element Prototype -
*max_element(first_iterator,
last_iterator)
Example –
vector <int> V{9,100,4,-98};
*max_element(V.begin(),V.end());
Min element
Algorithm
Min element Algorithm – To find
the minimum element of a
given vector.
*min_element Prototype –
*min_element(first_iterator,
last_iterator)
Example –
vector <int> V{54,345,23,1,-23};
*min_element(V.begin(),V.end());
Accumulate
Algorithm
Accumulate Algorithm – Returns
the summation of given Vector
Elements
accumulate Prototype –
accumulate(first_iterator,
last_iterator, initial value of sum)
Example –
vector <int> V{ 9,78,82,-8,0,12};
accumulate(V.begin(),V.end(),0);
Find Algorithm
Find Algorithm – Find value in a given range and
returns the iterator to the first occurrences of
value in each vector and points to last address of
vector V.end() if element is not present in the
same vector
find Prototype – find(initial_iterator,
final_iterator,value)
Example –
vector <int> V{ 8,7,-81,9,0,78};
find(V.begin(),V.end(),7);
Count Algorithm
Count Algorithm – returns the count of
occurrences of value in a given vector
count Prototype –
count(initial_iterator,
final_iterator,value)
Example –
vector <int> V{8,34,23,34,234,23,4,34};
count(V.begin(),V.end(),34);
References
• David Vandevoorde and Nicolai M. Josuttis (2002). C++
Templates: The Complete Guide. Addison-Wesley
Professional. ISBN 0-201-73484-2.
• Nicolai M. Josuttis (2000). The C++ Standard Library: A
Tutorial and Reference. Addison-Wesley. ISBN 0-201-37926-
0.
• Atul Saini and David R. Musser. STL Tutorial and Reference
Guide: C+ + Programming with the Standard Template
Library.
Thank You

More Related Content

What's hot (20)

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Java collection
Java collectionJava collection
Java collection
 
Data structure
Data structureData structure
Data structure
 
L11 array list
L11 array listL11 array list
L11 array list
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
 
Dictionaries and Sets in Python
Dictionaries and Sets in PythonDictionaries and Sets in Python
Dictionaries and Sets in Python
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
07 java collection
07 java collection07 java collection
07 java collection
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Python: The Iterator Pattern
Python: The Iterator PatternPython: The Iterator Pattern
Python: The Iterator Pattern
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Python decorators
Python decoratorsPython decorators
Python decorators
 
Sets in python
Sets in pythonSets in python
Sets in python
 

Similar to Standard Template Library

02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.pptYonas D. Ebren
 
Intro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysIntro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysBlue Elephant Consulting
 
Java Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxJava Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxrangariprajwal4554
 
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxAssg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxfestockton
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresJohn(Qiang) Zhang
 
Introduction data structure
Introduction data structureIntroduction data structure
Introduction data structureMuhammad Ismail
 
Collections in Java
Collections in JavaCollections in Java
Collections in JavaKhasim Cise
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
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
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingMandeep Singh
 

Similar to Standard Template Library (20)

02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
C++ STL 概觀
C++ STL 概觀C++ STL 概觀
C++ STL 概觀
 
Intro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysIntro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & Arrays
 
Java Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxJava Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptx
 
Python for Beginners
Python  for BeginnersPython  for Beginners
Python for Beginners
 
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxAssg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
 
Introduction data structure
Introduction data structureIntroduction data structure
Introduction data structure
 
2 b queues
2 b queues2 b queues
2 b queues
 
Python - Lecture 12
Python - Lecture 12Python - Lecture 12
Python - Lecture 12
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Collections
CollectionsCollections
Collections
 
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++
 
Standard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented ProgrammingStandard Template Library (STL) in Object Oriented Programming
Standard Template Library (STL) in Object Oriented Programming
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
c++ Unit III - PPT.pptx
 

Recently uploaded

GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 

Recently uploaded (20)

GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 

Standard Template Library

  • 2. What is a Standard Library ? In the C++ programming language, the Standard Library is a collection of classes and functions. The C++ Standard Library can be categorized into two parts − 1. The Standard Function Library 2. The Object-Oriented Class Library
  • 3. What are Templates ? • The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types. • C++ adds two new keywords to support templates: ‘template’ and ‘typename’. The second keyword can always be replaced by keyword ‘class’.
  • 4. How Templates work • Templates are expanded at compiler time. • The difference is, compiler does type checking before template expansion. • The source code contains only function/class, but compiled code may contain multiple copies of same function/class.
  • 5. What is Standard Template Library ? The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. A working knowledge of template classes is a prerequisite for working with STL.
  • 7. STL Component Overview Data Storage Containers • Hold Data Iterators • Access Data Algorithms • Manipulates Data Data Access Algorithms
  • 8. Containers • Containers or container classes store objects and data. • There are in total seven standard “first- class” container classes and three container adaptor classes and only seven header files that provide access to these containers or container adaptors.
  • 9. 3Types of Container Sequence Container Container Adapters Associative Container
  • 10. Sequence Container Implements data structures which can be accessed in a sequential manner. Vector List Deque
  • 11. What is Vector Class? • Vector is a template class in STL (Standard Template Library) of C++ programming language. • These are sequence containers that store elements. • They can automatically manage storage. It is efficient if you add and delete data often.
  • 12. Some Methods of Vector Class #include <vector> To import Vector Class size( ) Number of Elemets push_back( ) Appends at end pop_back( ) Erase last Element begin( ) Reference to first element end( ) Reference to end of Vector
  • 13. Example of Vector Class Vector <int> v = { 12 , 7 , 9 , 21 , 13 } ; 12 7 9 21 13 12 7 9 21 13 v.pop_bac k() ; 12 7 9 21 13 ….. 12 7 9 21 15 v.push_back( 15) ; 12 7 9 21 13 0 1 2 3 4 v.begin v[3 v.end(
  • 14. What is a List? • Sequence Containers - allow constant time insert and erase operations. • Store elements they contain in different and unrelated storage locations. • Lists - better in inserting, extracting and moving elements in any position within the container for which an iterator has already been obtained, and therefore also in algorithms that make intensive use of these, like sorting algorithms. • Consume some extra memory to keep the linking information associated to each element.
  • 15. Some Operations of List Class Operations Uses sort Sort elements in a list splice Transfer elements from 1 list to another list merge Merge sorted lists unique Removes duplicate elements swap Exchange the content of 1 list with that of another list. assign Assigns new contents to the list remove Erase all instances of value
  • 16. Example of List Class list <int> li = { 12 , 7 , 9 , 21 , 13 } ; 12 7 9 21 13 12 7 9 21 13 li.pop_bac k() ; 12 7 9 21 13 ….. 12 7 9 21 15 v.push_back( 15) ; 12 7 9 21 7 9 21 15 12 li.pop_fro nt() ; 14 7 9 21 15 ….. v.push_front( 14) ;
  • 17. Deque Class • Sequence containers with the feature of expansion and contraction on both the ends. • Implemented as dynamic arrays. • To use deque container in a program include statement: #include <deque> • Syntax: Deque declaration deque <elementType> name(size);
  • 18. Some Operations of Deque Class Operation Description insert Inserts an element. maxsize Return the max no of elements. pushfront Push elements into a deque from the front. pushback Push elements into a deque from the back. popfront Pop/remove elements from a deque from the front. popback Pop/remove elements from a deque from the back. clear Remove all the elements of the deque container.
  • 19. Container Adaptors Provide a different interface for sequential containers. Queue Priority Queue Stack
  • 20. Types of Container Adaptors • Queue: A queue can store elements in the sequence container list or deque data structure. • Priority queue: The priority queue adapter class is a little bit different in the sense that it enables the insertion of elements in sorted order into the data structure and removal of elements occurs from the front of the waiting line. • Stack: The class called stack is an implementation of the stack data structure where insertion and retrieval of elements occurs according to the FIFO manner
  • 21. Associative Containers Implement sorted data structures that can be quickly searched (O(log n) complexity). set multiset map multimap
  • 23. Iterators • What are iterators? • How many types of iterators are provided by C++ ? • Can iterators be null in C++ ?
  • 24. Iterators 17 17 23 12 17 4 array size 4 Iterators are pointer like entries that are used to access individual elements in the container Often, they are used to move sequentially from element to element, a process called iterating through container.
  • 25. Iterators 17 17 23 12 17 4 array size 4 The Member function begin() and end() return an iterator to the first and past the last element of a container 17 v.begin () 12 v.end()
  • 26. Algorithms • Collection of functions especially designed to be used on ranges of elements. • They act on containers and provide means for various operations for the contents of the containers. • Range - Sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers.
  • 27. Some of the most used Algorithms Sort Algorithm Reverse Algorithm Max element Algorithm Min element Algorithm Accumulate Algorithm Find Algorithm Count Algorithm
  • 28. Sort Algorithm Sort algorithm - To sort a given Vector sort Prototype - sort(first_iterator, last_iterator) Example – vector <int> V{ 10,20,40,15}; sort(V.begin(),V.end());
  • 29. Reverse Algorithm Reverse Algorithm – To reverse a given vector Reverse Prototype – reverse(first_iterator, last_iterator) Example – vector <int> V{ 12,8,9,15,7}; reverse(V.begin(),V.end());
  • 30. Max element Algorithm Max element Algorithm – To find the maximum element of a given vector. *max_element Prototype - *max_element(first_iterator, last_iterator) Example – vector <int> V{9,100,4,-98}; *max_element(V.begin(),V.end());
  • 31. Min element Algorithm Min element Algorithm – To find the minimum element of a given vector. *min_element Prototype – *min_element(first_iterator, last_iterator) Example – vector <int> V{54,345,23,1,-23}; *min_element(V.begin(),V.end());
  • 32. Accumulate Algorithm Accumulate Algorithm – Returns the summation of given Vector Elements accumulate Prototype – accumulate(first_iterator, last_iterator, initial value of sum) Example – vector <int> V{ 9,78,82,-8,0,12}; accumulate(V.begin(),V.end(),0);
  • 33. Find Algorithm Find Algorithm – Find value in a given range and returns the iterator to the first occurrences of value in each vector and points to last address of vector V.end() if element is not present in the same vector find Prototype – find(initial_iterator, final_iterator,value) Example – vector <int> V{ 8,7,-81,9,0,78}; find(V.begin(),V.end(),7);
  • 34. Count Algorithm Count Algorithm – returns the count of occurrences of value in a given vector count Prototype – count(initial_iterator, final_iterator,value) Example – vector <int> V{8,34,23,34,234,23,4,34}; count(V.begin(),V.end(),34);
  • 35. References • David Vandevoorde and Nicolai M. Josuttis (2002). C++ Templates: The Complete Guide. Addison-Wesley Professional. ISBN 0-201-73484-2. • Nicolai M. Josuttis (2000). The C++ Standard Library: A Tutorial and Reference. Addison-Wesley. ISBN 0-201-37926- 0. • Atul Saini and David R. Musser. STL Tutorial and Reference Guide: C+ + Programming with the Standard Template Library.