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

9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++Lahiru Dilshan
 
Access modifiers
Access modifiersAccess modifiers
Access modifiersJadavsejal
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST Kathirvel Ayyaswamy
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional arrayRajendran
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptxvishal choudhary
 
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.CIIT Atd.
 
Standard template library
Standard template libraryStandard template library
Standard template librarySukriti Singh
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptxvishal choudhary
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++Jayant Dalvi
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaEdureka!
 

What's hot (20)

9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
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.
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
File in C language
File in C languageFile in C language
File in C language
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Queues
QueuesQueues
Queues
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 

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
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to DatastructureNilesh Dalvi
 
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
 
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
 
13. Queue
13. Queue13. Queue
13. Queue
 
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
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to Datastructure
 
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
 
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
 

More from Nilesh Dalvi

6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception HandlingNilesh Dalvi
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and IntefacesNilesh Dalvi
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and MethodsNilesh Dalvi
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and VariablesNilesh Dalvi
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of JavaNilesh 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 (18)

14. Linked List
14. Linked List14. Linked List
14. Linked List
 
12. Stack
12. Stack12. Stack
12. Stack
 
11. Arrays
11. Arrays11. Arrays
11. Arrays
 
8. String
8. String8. String
8. String
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
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
 
Templates
TemplatesTemplates
Templates
 
File handling
File handlingFile handling
File handling
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
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

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
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
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Recently uploaded (20)

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
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
 
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
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

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.!!!