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

What's hot (20)

Maps
MapsMaps
Maps
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Stl Containers
Stl ContainersStl Containers
Stl Containers
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Python recursion
Python recursionPython recursion
Python recursion
 
Array
ArrayArray
Array
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Oops concept in c++ unit 3 -topic 4
Oops concept in c++ unit 3 -topic 4Oops concept in c++ unit 3 -topic 4
Oops concept in c++ unit 3 -topic 4
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Lecture 5 sorting and searching
Lecture 5   sorting and searchingLecture 5   sorting and searching
Lecture 5 sorting and searching
 
Queues
QueuesQueues
Queues
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Circular queue
Circular queueCircular queue
Circular queue
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
 

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

Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Recently uploaded (20)

Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

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.