SlideShare una empresa de Scribd logo
1 de 17
Standard Template Library (STL)
In C++
Mandeep Singh
Asst. Prof.
HMRITM (GGSIPU)
Standard Template Library
The C++ STL (Standard Template Library) is a
powerful set of C++ template classes to
provide general-purpose classes and
functions.
With templates that implement many popular
and commonly used algorithms and data
structures like lists, queues, and stacks.
C++ Standard Template Library are following three well-
structured components −
Sr. No Component & Description
1 Containers: (Organizing stored data in memory)
Containers are used to manage collections of objects of a certain kind.
There are several different types of containers like deque, list, vector,
map etc.
2 Algorithms: (Procedures applied to process their data)
Algorithms act on containers. They provide the means by which you will
perform initialization, sorting, searching, and transforming of the
contents of containers.
3 Iterators: (Point to the elements in a container)
Iterators are used to step through the elements of collections of
objects. These collections may be containers or subsets of containers.
STL Containers
1) Sequence container: vector, deque, list
2) Associative container: map, multimap, set , multiset,
3) Un-ordered associative container: unordered-map,
unordered-multimap, unordered-set, unordered-multiset
4) Container adapter: queue, priority queue, stack
Sequence container
-It is used to implement data structure which can
be accessed in a sequential manner.
Vector- It is same as dynamic arrays offer random
access back insertion.
Eg: int array[5]= {12,7,9,21,13};
Vector <int> v(array, array +5);
12,7,9,21,13 // V[3]=21
Functions of vector:- Push(), Pop().
V. Push_back(15); V.Pop_back(15);
12 7 9 21 13 15 12 7 9 21 13
//Program to demonstrate the vector container
#include<iostream>
#include<vector>
Using namespace std;
Int main()
{
Vector <int> V;
V. Push_back(1); //insert 1 at the back of V
V. Push_back(2); //insert 2 at the back of V
V. Push_back(4); //insert 4 at the back of V
For (Vector<int>:: iterator i=V, begin (1);
i!= V. End(); i++)
{
Cout <<*i <<””; // for printing the vector particular address location
}
}
O/P: 1 2 4
Vector Container Continue…
 Program that demonstrates the vector
container which is similar to an array with an
exception that it automatically handles its
own storage requirements in case it grows.
//Program to demonstrates the vector container:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int vector<int> vec; // create a vector to store
int i;
cout << "vector size = " << vec.size() << endl; // display the original size of vec
for(i = 0; i < 5; i++) // push 5 values into the vector
{vec.push_back(i); }
cout << "extended vector size = " << vec.size() ; // display extended size of vec
for(i = 0; i < 5; i++) { // access 5 values from the vector
cout << "value of vec [" << i << "] = " << vec[i] << endl; }
vector<int>::iterator v = vec.begin(); // use iterator to access the values
while( v != vec.end()) {
cout << "value of v = " << *v << endl;
v++; }
return 0; }
Associative and unordered associative containers
• Associative containers:
-used to implement stored data structure
Eg. Map, multimap, set, multiset
Eg. (1) map: maps are used to replicate associative arrays. It
contains stored key values pair, in which each key is unique,
and cannot be changed , it can insert and delete but cannot
alter.
O[n] -time complexity
Syntax: Map <key-type, value- type> map-name;
keys values
125 xyz
//Creating a map in different ways
#include<iostream>
#include<map>
Using namespace std;
int main ()
{
Map <int, int> m{[1,2],[2,3],[3,4]};
// creates a map in with keys 1,2,3 and their corresponding values 2,3,4
Map < string , int> map1;
// creates a map with keys of type char & Value of type integer
Map1 [“abc”]=100; // insert key=”abc” with value =100
Map1 [“b”]=200; // insert key=”b” with value=200
Map1 [“c”]=300; // insert key =”c” with value =300
Map1 [“def”]=400; // insert key=”def” with value=400
Map <char, int> map2 (map1. Begin(),Map1. End()); //creates a map map2
which have entries copied from map1. Begin() to map1. End()
}
Container adapter:
-It is used to provide different interface to the
sequence container
Eg. Queue , priority queue, stack
Queue: queue container is used to replicate queue in
c++, insertion always back, deletion always front of
queue
Syntax: queue <obj-type> queue-name;
Function: Push (), Pop(), size(), swap()
Eg. Back of queue front of queue2
//Program to demonstrate the Queue container
#include<iostream>
#include<queue>
Using namespace std;
int main()
{
queue <int> q; // creates an empty queue of int q
q. push(2); // Pushes 2 in the queue, now front=back=2
q. push(3); //Pushes 3 in the queue, now front=2, back=3
q.push(5);
}
O/P: 5 3 2
Priority queue
-It is just like a normal queue except the element
removed from the queue is always the
greatest among all the element in queue.
• Syntax: priority_queue <int> pq;
• Function: push(), Pop(), size(), swap()
//Program to demonstrate the Priority queue container
#include< iostream>
#include<queue>
Using namespace std;
int main()
{
priority_queue <int> pq;
Pq. Push (30); //insert 30 to pq now top=30
Pq. Push (40); // insert 40 to pq now top=40
Pq. Push (90); // insert 90 to pq now top =90
Pq. Push (60); // insert 60 to pq top stills 90
Pq. Pop () ; // removes 90
}
Stack:
Stack:
- insertion and deletion always performed at
top of the stack.
Syntax: stack<obj-type> stack_name;
Function: push (), pop(), top(), size(), swap()
//Program to demonstrate the Stack
#include<iostream>
#include <stack>
Using namespace std;
int main()
{
Stack <int> s; //create empty stack
S. push (2); //pushing element into stack
S. push(3);
S. push(4);
Cout << s.top(); //print 4
Cout << s. size(); //print 3
}
Thank You!!

Más contenido relacionado

La actualidad más candente

Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c languagekiran Patel
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocationRohit Shrivastava
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in cPrabhu Govind
 
Class 8b: Numpy & Matplotlib
Class 8b: Numpy & MatplotlibClass 8b: Numpy & Matplotlib
Class 8b: Numpy & MatplotlibMarc Gouw
 
Python advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmPython advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmJohn(Qiang) Zhang
 
The Ring programming language version 1.8 book - Part 86 of 202
The Ring programming language version 1.8 book - Part 86 of 202The Ring programming language version 1.8 book - Part 86 of 202
The Ring programming language version 1.8 book - Part 86 of 202Mahmoud Samir Fayed
 
16858 memory management2
16858 memory management216858 memory management2
16858 memory management2Aanand Singh
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocationFrijo Francis
 
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
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 

La actualidad más candente (20)

Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c language
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocation
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Functions with heap and stack
Functions with heap and stackFunctions with heap and stack
Functions with heap and stack
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 
Class 8b: Numpy & Matplotlib
Class 8b: Numpy & MatplotlibClass 8b: Numpy & Matplotlib
Class 8b: Numpy & Matplotlib
 
Python advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithmPython advanced 3.the python std lib by example – algorithm
Python advanced 3.the python std lib by example – algorithm
 
Algo>Queues
Algo>QueuesAlgo>Queues
Algo>Queues
 
The Ring programming language version 1.8 book - Part 86 of 202
The Ring programming language version 1.8 book - Part 86 of 202The Ring programming language version 1.8 book - Part 86 of 202
The Ring programming language version 1.8 book - Part 86 of 202
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
Stack Data structure
Stack Data structureStack Data structure
Stack Data structure
 
16858 memory management2
16858 memory management216858 memory management2
16858 memory management2
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocation
 
C dynamic ppt
C dynamic pptC dynamic ppt
C dynamic ppt
 
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
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 

Similar a Standard Template Library (STL) in Object Oriented Programming

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
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxAbhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxGauravPandey43518
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxbradburgess22840
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryGauravPatil318
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++•sreejith •sree
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notesGOKULKANNANMMECLECTC
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfaromalcom
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatchcqtt191
 
Lecture no 9.ppt operating system semester four
Lecture  no 9.ppt operating system semester fourLecture  no 9.ppt operating system semester four
Lecture no 9.ppt operating system semester fourVaibhavBhagwat18
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structurelodhran-hayat
 
Actionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptActionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptOUM SAOKOSAL
 

Similar a Standard Template Library (STL) in Object Oriented Programming (20)

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
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
C_STL_2.pptx
C_STL_2.pptxC_STL_2.pptx
C_STL_2.pptx
 
2 b queues
2 b queues2 b queues
2 b queues
 
Python - Lecture 12
Python - Lecture 12Python - Lecture 12
Python - Lecture 12
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatch
 
Python for Beginners
Python  for BeginnersPython  for Beginners
Python for Beginners
 
STL in C++
STL in C++STL in C++
STL in C++
 
05-stack_queue.ppt
05-stack_queue.ppt05-stack_queue.ppt
05-stack_queue.ppt
 
Lecture no 9.ppt operating system semester four
Lecture  no 9.ppt operating system semester fourLecture  no 9.ppt operating system semester four
Lecture no 9.ppt operating system semester four
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Actionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptActionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core Concept
 

Más de Mandeep Singh

Más de Mandeep Singh (11)

9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & Searching
 
8. Hash table
8. Hash table8. Hash table
8. Hash table
 
7. Spanning trees
7. Spanning trees7. Spanning trees
7. Spanning trees
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
1. Data structures introduction
1. Data structures introduction1. Data structures introduction
1. Data structures introduction
 
Ip6 tables in linux
Ip6 tables in linuxIp6 tables in linux
Ip6 tables in linux
 
Iptables in linux
Iptables in linuxIptables in linux
Iptables in linux
 

Último

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 

Último (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

Standard Template Library (STL) in Object Oriented Programming

  • 1. Standard Template Library (STL) In C++ Mandeep Singh Asst. Prof. HMRITM (GGSIPU)
  • 2. Standard Template Library The C++ STL (Standard Template Library) is a powerful set of C++ template classes to provide general-purpose classes and functions. With templates that implement many popular and commonly used algorithms and data structures like lists, queues, and stacks.
  • 3. C++ Standard Template Library are following three well- structured components − Sr. No Component & Description 1 Containers: (Organizing stored data in memory) Containers are used to manage collections of objects of a certain kind. There are several different types of containers like deque, list, vector, map etc. 2 Algorithms: (Procedures applied to process their data) Algorithms act on containers. They provide the means by which you will perform initialization, sorting, searching, and transforming of the contents of containers. 3 Iterators: (Point to the elements in a container) Iterators are used to step through the elements of collections of objects. These collections may be containers or subsets of containers.
  • 4. STL Containers 1) Sequence container: vector, deque, list 2) Associative container: map, multimap, set , multiset, 3) Un-ordered associative container: unordered-map, unordered-multimap, unordered-set, unordered-multiset 4) Container adapter: queue, priority queue, stack
  • 5. Sequence container -It is used to implement data structure which can be accessed in a sequential manner. Vector- It is same as dynamic arrays offer random access back insertion. Eg: int array[5]= {12,7,9,21,13}; Vector <int> v(array, array +5); 12,7,9,21,13 // V[3]=21 Functions of vector:- Push(), Pop(). V. Push_back(15); V.Pop_back(15); 12 7 9 21 13 15 12 7 9 21 13
  • 6. //Program to demonstrate the vector container #include<iostream> #include<vector> Using namespace std; Int main() { Vector <int> V; V. Push_back(1); //insert 1 at the back of V V. Push_back(2); //insert 2 at the back of V V. Push_back(4); //insert 4 at the back of V For (Vector<int>:: iterator i=V, begin (1); i!= V. End(); i++) { Cout <<*i <<””; // for printing the vector particular address location } } O/P: 1 2 4
  • 7. Vector Container Continue…  Program that demonstrates the vector container which is similar to an array with an exception that it automatically handles its own storage requirements in case it grows.
  • 8. //Program to demonstrates the vector container: #include <iostream> #include <vector> using namespace std; int main() { int vector<int> vec; // create a vector to store int i; cout << "vector size = " << vec.size() << endl; // display the original size of vec for(i = 0; i < 5; i++) // push 5 values into the vector {vec.push_back(i); } cout << "extended vector size = " << vec.size() ; // display extended size of vec for(i = 0; i < 5; i++) { // access 5 values from the vector cout << "value of vec [" << i << "] = " << vec[i] << endl; } vector<int>::iterator v = vec.begin(); // use iterator to access the values while( v != vec.end()) { cout << "value of v = " << *v << endl; v++; } return 0; }
  • 9. Associative and unordered associative containers • Associative containers: -used to implement stored data structure Eg. Map, multimap, set, multiset Eg. (1) map: maps are used to replicate associative arrays. It contains stored key values pair, in which each key is unique, and cannot be changed , it can insert and delete but cannot alter. O[n] -time complexity Syntax: Map <key-type, value- type> map-name; keys values 125 xyz
  • 10. //Creating a map in different ways #include<iostream> #include<map> Using namespace std; int main () { Map <int, int> m{[1,2],[2,3],[3,4]}; // creates a map in with keys 1,2,3 and their corresponding values 2,3,4 Map < string , int> map1; // creates a map with keys of type char & Value of type integer Map1 [“abc”]=100; // insert key=”abc” with value =100 Map1 [“b”]=200; // insert key=”b” with value=200 Map1 [“c”]=300; // insert key =”c” with value =300 Map1 [“def”]=400; // insert key=”def” with value=400 Map <char, int> map2 (map1. Begin(),Map1. End()); //creates a map map2 which have entries copied from map1. Begin() to map1. End() }
  • 11. Container adapter: -It is used to provide different interface to the sequence container Eg. Queue , priority queue, stack Queue: queue container is used to replicate queue in c++, insertion always back, deletion always front of queue Syntax: queue <obj-type> queue-name; Function: Push (), Pop(), size(), swap() Eg. Back of queue front of queue2
  • 12. //Program to demonstrate the Queue container #include<iostream> #include<queue> Using namespace std; int main() { queue <int> q; // creates an empty queue of int q q. push(2); // Pushes 2 in the queue, now front=back=2 q. push(3); //Pushes 3 in the queue, now front=2, back=3 q.push(5); } O/P: 5 3 2
  • 13. Priority queue -It is just like a normal queue except the element removed from the queue is always the greatest among all the element in queue. • Syntax: priority_queue <int> pq; • Function: push(), Pop(), size(), swap()
  • 14. //Program to demonstrate the Priority queue container #include< iostream> #include<queue> Using namespace std; int main() { priority_queue <int> pq; Pq. Push (30); //insert 30 to pq now top=30 Pq. Push (40); // insert 40 to pq now top=40 Pq. Push (90); // insert 90 to pq now top =90 Pq. Push (60); // insert 60 to pq top stills 90 Pq. Pop () ; // removes 90 }
  • 15. Stack: Stack: - insertion and deletion always performed at top of the stack. Syntax: stack<obj-type> stack_name; Function: push (), pop(), top(), size(), swap()
  • 16. //Program to demonstrate the Stack #include<iostream> #include <stack> Using namespace std; int main() { Stack <int> s; //create empty stack S. push (2); //pushing element into stack S. push(3); S. push(4); Cout << s.top(); //print 4 Cout << s. size(); //print 3 }