SlideShare a Scribd company logo
1 of 32
OObbjjeecctt oorriieenntteedd PPrrooggrraammmmiinngg 
wwiitthh CC++++ 
SSttaannddaarrdd TTeemmppllaattee LLiibbrraarryy 
By 
Nilesh Dalvi 
LLeeccttuurreerr,, PPaattkkaarr--VVaarrddee CCoolllleeggee.. 
http://www.slideshare.net/nileshdalvi01
Introduction To STL 
• STL is an advanced application of templates. 
• Introduced by Meng Lee and Alexander Stepanov of 
Hewlwtt-packard in 1994. 
• Contains several built-in functions and operators that 
help the programmer to develop complex programs. 
• All famous compiler provides provide the STL. 
• It consists of vectors, lists, queues and stacks. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Why should I use STL? 
• STL can save considerable time and effort, and lead 
to high quality programs. 
• These benefits are possible because we are “reusing” 
the well-written and well-tested components 
defined in the STL. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
STL components overview 
• Data storage, data 
access and algorithms 
are separated 
– Containers hold data 
– Iterators access data 
– Algorithms, function 
objects manipulate data 
– Allocators... allocate 
data (mostly, we ignore 
them) 
Container Container 
Algorithm 
Container 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Container 
• A container is a way that stored data is 
organized in memory, for example an array of 
elements. 
Container 
Sequential Associative Container Adapters 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Container 
• Sequence containers 
– vector 
– deque 
– list 
• Associative containers 
– set 
– multiset 
– map 
– multimap 
• Container adapters 
– stack 
– queue 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Sequential Container 
• STL sequence containers allows controlled sequential 
access to elements. 
• It hold data elements in linear series. 
• Every elements is associated by its location in series. 
Item A Item B Item C 
Item D 
Iterator 
start() end() 
Fig.: Data elements in sequence container 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Sequential Container 
– vector<T> – dynamic array 
• Offers random access 
• Allows insertion and deletions at back 
• Backward compatible with C : &v[0] points to the first 
element 
– deque<T> – double-ended queue (usually array of 
arrays) 
• Offers random access, back and front insertion 
• Slower than vectors, no C compatibility 
– list<T> – 'traditional' doubly linked list 
• Don't expect random access, you can insert anywhere 
though 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Some functions of vector class 
-size() 
-provides the number of elements 
-push_back() 
-appends an element to the end 
-pop_back() 
-Erases the last element 
-begin() 
-Provides reference to starting element 
-end() 
-Provides reference to end of vector 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Vector container 
int array[5] = {12, 7, 9, 21, 13 }; 
vector<int> v(array,array+5); 
12 7 9 21 13 
0 1 2 3 4 
v.begin(); 
12 7 9 21 
13 
v.push_back(15); 
12 7 9 21 
… 
15 
12 7 9 21 15 
v[3] 
v.pop_back(); 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Vector container 
#include <iostream> 
#include <vector> 
using namespace std; 
void display(vector <int> &v) 
{ 
for (int i=0; i < v.size(); i++) 
{ 
cout << v[i] << " "; 
} 
cout << "n"; 
} 
int main() 
{ 
vector <int> v; 
cout << "initial size = "<< v.size() << "n"; 
int x; 
cout << "Enter the 5 int valuesn"; 
for (int i=0; i < 5; i++) 
{ 
cin >> x; 
v.push_back(x); 
} 
cout << "nsize after adding = "<< v.size() << "n"; 
cout << "Current Vector::n"; 
display(v); 
return 0; 
} 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Vector container 
Output: 
initial size = 0 
Enter the 5 int values 
11 
22 
33 
44 
55 
size after adding = 5 
Current Vector:: 
11 22 33 44 55 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Some function of list class 
• list functions for object t 
– t.sort() 
• Sorts in ascending order 
– t.insert() 
• Inserts given element 
– t.merge() 
• Combines to sorted lists 
– t.unique() 
• Removes identical elements in the lists. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Functions of list class 
• list functions 
– t.swap(otherObject); 
• Exchange contents 
– t.front() 
• Erases the last elements 
– t.remove(value) 
• Erases all instances of value 
– t.empty() 
• Determines the list is vacant or not 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
List container 
int array[5] = {12, 7, 9, 21, 13 }; 
list<int> li(array,array+5); 
12 9 21 
7 9 21 
12 
li.push_front(8); 
12 7 9 21 
… 
15 
li.pop_front(); 
13 
li.push_back(15); 
12 7 9 21 
… 
15 
li.pop_back(); 
8 
li.insert() 
19 
7 12 17 21 23 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
List container 
#include <iostream> 
#include <list> 
using namespace std; 
void show(list <int> &num) 
{ 
list<int> :: iterator n; 
for (n = num.begin(); n != num.end(); 
++n) 
cout << *n << " "; 
} 
int main() 
{ 
list <int> list; 
list .push_back(5); 
list .push_back(10); 
list .push_back(15); 
list .push_back(20); 
cout << "Numbers are ::"; 
show(list); 
list .push_front(1); 
list .push_back(25); 
cout << "nAfter adding Numbers are ::"; 
show(list); 
return 0; 
} 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
List container 
Output: 
Numbers are ::5 10 15 20 
After adding Numbers are ::1 5 10 15 20 25 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
List container : sort() 
int main() 
{ 
list <int> list; 
list .push_back(5); 
list .push_back(10); 
list .push_back(15); 
list .push_back(20); 
cout << "Unsorted list :"; 
show(list); 
cout << "nSorted list :"; 
list.sort(); 
show(list); 
return 0; 
} 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Associative Container 
• It is non-sequential but uses a key to access 
elements. 
• The keys, typically a number or a string, are used by 
the container to arrange the stored elements in a 
specific order 
• For example in a dictionary the entries are ordered 
alphabetically. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Associative Containers 
– The sorting criterion is also a template parameter 
– set<T> – the item stored act as key, no duplicates 
– multiset<T> – set allowing duplicate items 
– map<K,V> – separate key and value, no duplicates 
– multimap<K,V> – map allowing duplicate keys 
– hashed associative containers may be available 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Maps 
• It is series of pairs of key names and values associated 
with it. 
• Access data values depends upon the key and it is very 
quick. 
• Must specify the key to get the corresponding value. 
Key B Key C 
Key n 
Value B Value C Value n 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Some functions of maps class 
-clear() 
-Removes all elements from the map 
-erase() 
-Removes the given element 
-insert() 
-Inserts the element as given 
-begin() 
-Provides reference to starting element 
-end() 
-Provides reference to end of map 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Some functions of maps class 
-empty() 
-Determines whether the map is vacant or not 
-size() 
-Provides the size of the map 
-find() 
-Provides the location of the given element 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Maps container 
#include <iostream> 
#include <map> 
#include <string> 
using namespace std; 
int main() 
{ 
typedef map<string, int> SYIT; 
SYIT stu; 
stu["ABC"] = 111; 
stu["PQR"] = 222; 
stu["XYZ"] = 333; 
stu["MNO"] = 444; 
SYIT::iterator pos; 
for (pos = stu.begin(); pos != stu.end(); ++pos) 
{ 
cout << "key: "" << pos->first << "" " 
<< "value: " << pos->second << endl; 
} 
return 0; 
} 
Output: 
key: "ABC" value: 111 
key: "MNO" value: 444 
key: "PQR" value: 222 
key: "XYZ" value: 333 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Container adaptors 
• Container adapters 
– stack, queue and priority_queue 
– Not first class containers 
• Do not support iterators 
• Do not provide actual data structure 
– Programmer can select implementation 
– Member functions push and pop 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithms 
Algorithms in the STL are procedures 
that are applied to containers to process 
their data, for example search for an 
element in an array, or sort an array. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
For_Each() Algorithm 
#include <vector> 
#include <algorithm> 
#include <iostream> 
void show(int n) 
{ 
cout << n << ” ”; 
} 
int arr[] = { 12, 3, 17, 8 }; // standard C array 
vector<int> v(arr, arr+4); // initialize vector with C array 
for_each (v.begin(), v.end(), show); // apply function show 
// to each element of vector v 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Find() Algorithm 
#include <vector> 
#include <algorithm> 
#include <iostream> 
int key; 
int arr[] = { 12, 3, 17, 8, 34, 56, 9 }; // standard C array 
vector<int> v(arr, arr+7); // initialize vector with C array 
vector<int>::iterator iter; 
cout << ”enter value :”; 
cin >> key; 
iter=find(v.begin(),v.end(),key); // finds integer key in v 
if (iter != v.end()) // found the element 
cout << ”Element ” << key << ” found” << endl; 
else 
cout << ”Element ” << key << ” not in vector v” << endl; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Sort & Merge 
• Sort and merge allow you to sort and merge 
elements in a container 
#include <list> 
int arr1[]= { 6, 4, 9, 1, 7 }; 
int arr2[]= { 4, 2, 1, 3, 8 }; 
list<int> l1(arr1, arr1+5); // initialize l1 with arr1 
list<int> l2(arr2, arr2+5); // initialize l2 with arr2 
l1.sort(); // l1 = {1, 4, 6, 7, 9} 
l2.sort(); // l2= {1, 2, 3, 4, 8 } 
l1.merge(l2); // merges l2 into l1 
// l1 = { 1, 1, 2, 3, 4, 4, 6, 7, 8, 9}, l2= {} Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Iterators 
• Iterators are pointer-like entities that are used to 
access individual elements in a container. 
• Often they are used to move sequentially from 
element to element, a process called iterating 
through a container. 
vector<int> 
array_ 17 
4 
23 
12 
size_ 4 
vector<int>::iterator 
The iterator corresponding to 
the class vector<int> is of 
the type vector<int>::iterator 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Iterators 
• The member functions begin() and end() return an 
iterator to the first and past the last element of a 
container 
arravye_ctor<int> v 17 
4 
23 
12 
size_ 4 
v.begin() 
v.end() 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
So long and thanks for all the attention  
THANK YOU.!!!

More Related Content

What's hot

3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and VariablesNilesh Dalvi
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and IntefacesNilesh Dalvi
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of JavaNilesh Dalvi
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structuresagorolabs
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 

What's hot (7)

3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Arrays in Java
Arrays in Java Arrays in Java
Arrays in Java
 

Viewers also liked

Operator Overloading In Scala
Operator Overloading In ScalaOperator Overloading In Scala
Operator Overloading In ScalaJoey Gibson
 
Function class in c++
Function class in c++Function class in c++
Function class in c++Kumar
 
7 inheritance and polymorphism
7   inheritance and polymorphism7   inheritance and polymorphism
7 inheritance and polymorphismTuan Ngo
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 

Viewers also liked (6)

Scala Class&object
Scala Class&objectScala Class&object
Scala Class&object
 
Operator Overloading In Scala
Operator Overloading In ScalaOperator Overloading In Scala
Operator Overloading In Scala
 
Function class in c++
Function class in c++Function class in c++
Function class in c++
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
7 inheritance and polymorphism
7   inheritance and polymorphism7   inheritance and polymorphism
7 inheritance and polymorphism
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 

Similar to Standard Template Library

Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2Kenji HASUNUMA
 
Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Kenji HASUNUMA
 
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
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File Harjinder Singh
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
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
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptadityavarte
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbRAtna29
 
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)

Strings
StringsStrings
Strings
 
Lec3
Lec3Lec3
Lec3
 
Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2
 
Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Collections Framework Begineers guide 2
Collections Framework Begineers guide 2
 
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
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
2 b queues
2 b queues2 b queues
2 b queues
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
Data structure
Data  structureData  structure
Data structure
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Lec3
Lec3Lec3
Lec3
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
 
Python - Lecture 12
Python - Lecture 12Python - Lecture 12
Python - Lecture 12
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
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
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 

More from Nilesh Dalvi

6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception HandlingNilesh Dalvi
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh Dalvi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cppNilesh Dalvi
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops conceptsNilesh Dalvi
 

More from Nilesh Dalvi (8)

7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 
Templates
TemplatesTemplates
Templates
 
File handling
File handlingFile handling
File handling
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 

Recently uploaded

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

Standard Template Library

  • 1. OObbjjeecctt oorriieenntteedd PPrrooggrraammmmiinngg wwiitthh CC++++ SSttaannddaarrdd TTeemmppllaattee LLiibbrraarryy By Nilesh Dalvi LLeeccttuurreerr,, PPaattkkaarr--VVaarrddee CCoolllleeggee.. http://www.slideshare.net/nileshdalvi01
  • 2. Introduction To STL • STL is an advanced application of templates. • Introduced by Meng Lee and Alexander Stepanov of Hewlwtt-packard in 1994. • Contains several built-in functions and operators that help the programmer to develop complex programs. • All famous compiler provides provide the STL. • It consists of vectors, lists, queues and stacks. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 3. Why should I use STL? • STL can save considerable time and effort, and lead to high quality programs. • These benefits are possible because we are “reusing” the well-written and well-tested components defined in the STL. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 4. STL components overview • Data storage, data access and algorithms are separated – Containers hold data – Iterators access data – Algorithms, function objects manipulate data – Allocators... allocate data (mostly, we ignore them) Container Container Algorithm Container Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 5. Container • A container is a way that stored data is organized in memory, for example an array of elements. Container Sequential Associative Container Adapters Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 6. Container • Sequence containers – vector – deque – list • Associative containers – set – multiset – map – multimap • Container adapters – stack – queue Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 7. Sequential Container • STL sequence containers allows controlled sequential access to elements. • It hold data elements in linear series. • Every elements is associated by its location in series. Item A Item B Item C Item D Iterator start() end() Fig.: Data elements in sequence container Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 8. Sequential Container – vector<T> – dynamic array • Offers random access • Allows insertion and deletions at back • Backward compatible with C : &v[0] points to the first element – deque<T> – double-ended queue (usually array of arrays) • Offers random access, back and front insertion • Slower than vectors, no C compatibility – list<T> – 'traditional' doubly linked list • Don't expect random access, you can insert anywhere though Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 9. Some functions of vector class -size() -provides the number of elements -push_back() -appends an element to the end -pop_back() -Erases the last element -begin() -Provides reference to starting element -end() -Provides reference to end of vector Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 10. Vector container int array[5] = {12, 7, 9, 21, 13 }; vector<int> v(array,array+5); 12 7 9 21 13 0 1 2 3 4 v.begin(); 12 7 9 21 13 v.push_back(15); 12 7 9 21 … 15 12 7 9 21 15 v[3] v.pop_back(); Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 11. Vector container #include <iostream> #include <vector> using namespace std; void display(vector <int> &v) { for (int i=0; i < v.size(); i++) { cout << v[i] << " "; } cout << "n"; } int main() { vector <int> v; cout << "initial size = "<< v.size() << "n"; int x; cout << "Enter the 5 int valuesn"; for (int i=0; i < 5; i++) { cin >> x; v.push_back(x); } cout << "nsize after adding = "<< v.size() << "n"; cout << "Current Vector::n"; display(v); return 0; } Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 12. Vector container Output: initial size = 0 Enter the 5 int values 11 22 33 44 55 size after adding = 5 Current Vector:: 11 22 33 44 55 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 13. Some function of list class • list functions for object t – t.sort() • Sorts in ascending order – t.insert() • Inserts given element – t.merge() • Combines to sorted lists – t.unique() • Removes identical elements in the lists. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 14. Functions of list class • list functions – t.swap(otherObject); • Exchange contents – t.front() • Erases the last elements – t.remove(value) • Erases all instances of value – t.empty() • Determines the list is vacant or not Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 15. List container int array[5] = {12, 7, 9, 21, 13 }; list<int> li(array,array+5); 12 9 21 7 9 21 12 li.push_front(8); 12 7 9 21 … 15 li.pop_front(); 13 li.push_back(15); 12 7 9 21 … 15 li.pop_back(); 8 li.insert() 19 7 12 17 21 23 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 16. List container #include <iostream> #include <list> using namespace std; void show(list <int> &num) { list<int> :: iterator n; for (n = num.begin(); n != num.end(); ++n) cout << *n << " "; } int main() { list <int> list; list .push_back(5); list .push_back(10); list .push_back(15); list .push_back(20); cout << "Numbers are ::"; show(list); list .push_front(1); list .push_back(25); cout << "nAfter adding Numbers are ::"; show(list); return 0; } Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 17. List container Output: Numbers are ::5 10 15 20 After adding Numbers are ::1 5 10 15 20 25 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 18. List container : sort() int main() { list <int> list; list .push_back(5); list .push_back(10); list .push_back(15); list .push_back(20); cout << "Unsorted list :"; show(list); cout << "nSorted list :"; list.sort(); show(list); return 0; } Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 19. Associative Container • It is non-sequential but uses a key to access elements. • The keys, typically a number or a string, are used by the container to arrange the stored elements in a specific order • For example in a dictionary the entries are ordered alphabetically. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 20. Associative Containers – The sorting criterion is also a template parameter – set<T> – the item stored act as key, no duplicates – multiset<T> – set allowing duplicate items – map<K,V> – separate key and value, no duplicates – multimap<K,V> – map allowing duplicate keys – hashed associative containers may be available Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 21. Maps • It is series of pairs of key names and values associated with it. • Access data values depends upon the key and it is very quick. • Must specify the key to get the corresponding value. Key B Key C Key n Value B Value C Value n Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 22. Some functions of maps class -clear() -Removes all elements from the map -erase() -Removes the given element -insert() -Inserts the element as given -begin() -Provides reference to starting element -end() -Provides reference to end of map Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 23. Some functions of maps class -empty() -Determines whether the map is vacant or not -size() -Provides the size of the map -find() -Provides the location of the given element Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 24. Maps container #include <iostream> #include <map> #include <string> using namespace std; int main() { typedef map<string, int> SYIT; SYIT stu; stu["ABC"] = 111; stu["PQR"] = 222; stu["XYZ"] = 333; stu["MNO"] = 444; SYIT::iterator pos; for (pos = stu.begin(); pos != stu.end(); ++pos) { cout << "key: "" << pos->first << "" " << "value: " << pos->second << endl; } return 0; } Output: key: "ABC" value: 111 key: "MNO" value: 444 key: "PQR" value: 222 key: "XYZ" value: 333 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 25. Container adaptors • Container adapters – stack, queue and priority_queue – Not first class containers • Do not support iterators • Do not provide actual data structure – Programmer can select implementation – Member functions push and pop Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 26. Algorithms Algorithms in the STL are procedures that are applied to containers to process their data, for example search for an element in an array, or sort an array. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 27. For_Each() Algorithm #include <vector> #include <algorithm> #include <iostream> void show(int n) { cout << n << ” ”; } int arr[] = { 12, 3, 17, 8 }; // standard C array vector<int> v(arr, arr+4); // initialize vector with C array for_each (v.begin(), v.end(), show); // apply function show // to each element of vector v Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 28. Find() Algorithm #include <vector> #include <algorithm> #include <iostream> int key; int arr[] = { 12, 3, 17, 8, 34, 56, 9 }; // standard C array vector<int> v(arr, arr+7); // initialize vector with C array vector<int>::iterator iter; cout << ”enter value :”; cin >> key; iter=find(v.begin(),v.end(),key); // finds integer key in v if (iter != v.end()) // found the element cout << ”Element ” << key << ” found” << endl; else cout << ”Element ” << key << ” not in vector v” << endl; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 29. Sort & Merge • Sort and merge allow you to sort and merge elements in a container #include <list> int arr1[]= { 6, 4, 9, 1, 7 }; int arr2[]= { 4, 2, 1, 3, 8 }; list<int> l1(arr1, arr1+5); // initialize l1 with arr1 list<int> l2(arr2, arr2+5); // initialize l2 with arr2 l1.sort(); // l1 = {1, 4, 6, 7, 9} l2.sort(); // l2= {1, 2, 3, 4, 8 } l1.merge(l2); // merges l2 into l1 // l1 = { 1, 1, 2, 3, 4, 4, 6, 7, 8, 9}, l2= {} Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 30. Iterators • Iterators are pointer-like entities that are used to access individual elements in a container. • Often they are used to move sequentially from element to element, a process called iterating through a container. vector<int> array_ 17 4 23 12 size_ 4 vector<int>::iterator The iterator corresponding to the class vector<int> is of the type vector<int>::iterator Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 31. Iterators • The member functions begin() and end() return an iterator to the first and past the last element of a container arravye_ctor<int> v 17 4 23 12 size_ 4 v.begin() v.end() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 32. So long and thanks for all the attention  THANK YOU.!!!