SlideShare a Scribd company logo
1 of 28
Standard Template Library
Prepared By
- Sreejith S -
- Rahul Babu R -
Contents
 Introduction To STL
 Containers
 Iterators
Algorithms
 Function Objects
Introduction To STL
• STL is Standard Template Library
– Powerful, template-based components
• Containers: template data structures
• Iterators: like pointers, access elements of containers
• Algorithms: data manipulation, searching, sorting, etc.
– Object- oriented programming: reuse, reuse,
reuse
– Only an introduction to STL, a huge class library
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
Container
• A container is a way that stored data is
organized in memory, for example an array of
elements.
Container
Sequential Associative Container Adapters
Container ctd-
• Sequence containers
– vector
– deque
– list
• Associative containers
– set
– multiset
– map
– multimap
• Container adapters
– stack
– queue
Sequential Container
– vector<T> – dynamic array
• Offers random access, back insertion
• Should be your default choice, but choose wisely
• 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
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 last element
-end()
-Provides reference to end of vector
Vector container
int array[5] = {12, 7, 9, 21, 13 };
Vector<int> v(array,array+5);
12 7 9 21 13
v.begin();
12 7 9 21
13
v.push_back(15);
12 7 9 21
…
15
12 7 9 21 15
v[3]
0 1 2 3 4
v.pop_back();
Some function of list class
• list functions for object t
– t.sort()
• Sorts in ascending order
– t.splice(iterator, otherObject );
• Inserts values from otherObject before iterator
– t.merge( otherObject )
• Removes otherObject and inserts it into t, sorted
– t.unique()
• Removes duplicate elements
Functions of list class cntd-
• list functions
– t.swap(otherObject);
• Exchange contents
– t.assign(iterator1, iterator2)
• Replaces contents with elements in range of iterators
– t.remove(value)
• Erases all instances of value
List container
int array[5] = {12, 7, 9, 21, 13 };
list<int> li(array,array+5);
7 9 21
12
li.push_front(8);
12 7 9 21
…
15
li.pop_front();
12 9 21
13
li.push_back(15);
12 7 9 21
…
15
li.pop_back();
8
7 12 17 21 23
li.insert()
19
Functions of dequeue class
• dequeue functions for object d
-d.front()
-Return a reference (or const_reference) to the first
component of d
-d.back()
-Return a reference (or const_reference) to the last
component of d.
-d.size()
-Return a value of type size_type giving the number of
values currently in d.
Functions of dequeue class contd-
-d.push_back(val)
-Add val to the end of d, increasing the size of d by one.
-d.push_front(val)
-Add val to the front of d, increasing the size of d by one.
-d.pop_back()
-Delete the last value of d. The size of d is reduced by
one.
-d.pop_front()
-Delete the first value of d. The size of d is reduced by
one.
Associative Containers
– Offer O(log n) insertion, suppression and access
– Store only weakly strict ordered types (eg. numeric
types)
• Must have operator<() and operator==() defined
and !(a<b) && !(b<a) (a==b)≡
– 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
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
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
Theiterator corresponding to
the class vector<int> is of
the typevector<int>::iterator
Iterators contd-
• The member functions begin() and end() return an
iterator to the first and past the last element of a
container
vector<int> v
array_ 17
4
23
12
size_ 4
v.end()
v.begin()
Iterators Categories
• Not every iterator can be used with every container for
example the list class provides no random access iterator
• Every algorithm requires an iterator with a certain level of
capability for example to use the [] operator you need a
random access iterator
• Iterators are divided into five categories in which a higher
(more specific) category always subsumes a lower (more
general) category, e.g. An algorithm that
accepts a forward iterator will also work with a bidirectional
iterator and a random access iterator
input
output
forward bidirectional
random
access
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.
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
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;
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= {}
Functions Objects
• Some algorithms like sort, merge, accumulate can take a
function object as argument.
• A function object is an object of a template class that has a
single member function : the overloaded operator ()
• It is also possible to use user-written functions in place of
pre-defined function objects
#include <list>
#include <functional>
int arr1[]= { 6, 4, 9, 1, 7 };
list<int> l1(arr1, arr1+5); // initialize l1 with arr1
l1.sort(greater<int>()); // uses function object greater<int>
// for sorting in reverse order l1 = { 9, 7, 6, 4, 1 }
Function Objects
• The accumulate algorithm accumulates data over the
elements of the containing, for example computing the sum of
elements
#include <list>
#include <functional>
#include <numeric>
int arr1[]= { 6, 4, 9, 1, 7 };
list<int> l1(arr1, arr1+5); // initialize l1 with arr1
int sum = accumulate(l1.begin(), l1.end() , 0, plus<int>());
int sum = accumulate(l1.begin(), l1.end(),0); // equivalent
int fac = accumulate(l1.begin(), l1.end() , 0, times<int>());
User Defined Function Objects
class squared _sum // user-defined function object
{
public:
int operator()(int n1, int n2) { return n1+n2*n2; }
};
int sq = accumulate(l1.begin(), l1.end() , 0,
squared_sum() );
// computes the sum of squares
THANK YOU.!!!
So long and thanks for all the attention 
?

More Related Content

What's hot

Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Stl Containers
Stl ContainersStl Containers
Stl Containersppd1961
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptxvishal choudhary
 
Standard template library
Standard template libraryStandard template library
Standard template librarySukriti Singh
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)Elavarasi K
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in javaUmamaheshwariv1
 
Java Serialization
Java SerializationJava Serialization
Java Serializationimypraz
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 

What's hot (20)

Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Stl Containers
Stl ContainersStl Containers
Stl Containers
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in java
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Java Serialization
Java SerializationJava Serialization
Java Serialization
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 

Viewers also liked

C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template LibraryIlio Catallo
 
Stl (standard template library)
Stl (standard template library)Stl (standard template library)
Stl (standard template library)Hemant Jain
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryKumar Gaurav
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-libraryHariz Mustafa
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template LibraryDuda Dornelles
 
Extreme Scale Breadth-First Search on Supercomputers
Extreme Scale Breadth-First Search on SupercomputersExtreme Scale Breadth-First Search on Supercomputers
Extreme Scale Breadth-First Search on SupercomputersToyotaro Suzumura
 
iterators-must-go
iterators-must-goiterators-must-go
iterators-must-goHiroshi Ono
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructorDa Mystic Sadi
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++ppd1961
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternNitin Bhide
 
The State of Twitter: STL 2011
The State of Twitter: STL 2011The State of Twitter: STL 2011
The State of Twitter: STL 2011Infuz
 
NUMA optimized Parallel Breadth first Search on Multicore Single node System
NUMA optimized Parallel Breadth first Search on Multicore Single node SystemNUMA optimized Parallel Breadth first Search on Multicore Single node System
NUMA optimized Parallel Breadth first Search on Multicore Single node SystemMohammad Tahsin Alshalabi
 

Viewers also liked (20)

C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
Stl (standard template library)
Stl (standard template library)Stl (standard template library)
Stl (standard template library)
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-library
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
Csrf final
Csrf finalCsrf final
Csrf final
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 
Savitch ch 18
Savitch ch 18Savitch ch 18
Savitch ch 18
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
Extreme Scale Breadth-First Search on Supercomputers
Extreme Scale Breadth-First Search on SupercomputersExtreme Scale Breadth-First Search on Supercomputers
Extreme Scale Breadth-First Search on Supercomputers
 
iterators-must-go
iterators-must-goiterators-must-go
iterators-must-go
 
1.5.5 stl
1.5.5 stl1.5.5 stl
1.5.5 stl
 
Stl design overview
Stl design overviewStl design overview
Stl design overview
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
C++ STL 概觀
C++ STL 概觀C++ STL 概觀
C++ STL 概觀
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design pattern
 
Iterator
IteratorIterator
Iterator
 
The State of Twitter: STL 2011
The State of Twitter: STL 2011The State of Twitter: STL 2011
The State of Twitter: STL 2011
 
NUMA optimized Parallel Breadth first Search on Multicore Single node System
NUMA optimized Parallel Breadth first Search on Multicore Single node SystemNUMA optimized Parallel Breadth first Search on Multicore Single node System
NUMA optimized Parallel Breadth first Search on Multicore Single node System
 

Similar to standard template library(STL) in C++

Collections in Java
Collections in JavaCollections in Java
Collections in JavaKhasim Cise
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from javaIndicThreads
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.pptYonas D. Ebren
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3Ahmet Bulut
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
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
 
Proposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyondProposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyondBarry Feigenbaum
 
Standard template library
Standard template libraryStandard template library
Standard template libraryJancypriya M
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...gjcross
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks QueuesIntro C# Book
 

Similar to standard template library(STL) in C++ (20)

Collections
CollectionsCollections
Collections
 
Collections
CollectionsCollections
Collections
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
 
Queue
QueueQueue
Queue
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from java
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
R training2
R training2R training2
R training2
 
2 b queues
2 b queues2 b queues
2 b queues
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
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
 
Proposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyondProposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyond
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
collections
collectionscollections
collections
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 

Recently uploaded

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
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
 
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
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
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
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
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
 
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
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
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
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 

Recently uploaded (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
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
 
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...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
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Ă...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).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
 
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
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

standard template library(STL) in C++

  • 1. Standard Template Library Prepared By - Sreejith S - - Rahul Babu R -
  • 2. Contents  Introduction To STL  Containers  Iterators Algorithms  Function Objects
  • 3. Introduction To STL • STL is Standard Template Library – Powerful, template-based components • Containers: template data structures • Iterators: like pointers, access elements of containers • Algorithms: data manipulation, searching, sorting, etc. – Object- oriented programming: reuse, reuse, reuse – Only an introduction to STL, a huge class library
  • 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
  • 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
  • 6. Container ctd- • Sequence containers – vector – deque – list • Associative containers – set – multiset – map – multimap • Container adapters – stack – queue
  • 7. Sequential Container – vector<T> – dynamic array • Offers random access, back insertion • Should be your default choice, but choose wisely • 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
  • 8. 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 last element -end() -Provides reference to end of vector
  • 9. Vector container int array[5] = {12, 7, 9, 21, 13 }; Vector<int> v(array,array+5); 12 7 9 21 13 v.begin(); 12 7 9 21 13 v.push_back(15); 12 7 9 21 … 15 12 7 9 21 15 v[3] 0 1 2 3 4 v.pop_back();
  • 10. Some function of list class • list functions for object t – t.sort() • Sorts in ascending order – t.splice(iterator, otherObject ); • Inserts values from otherObject before iterator – t.merge( otherObject ) • Removes otherObject and inserts it into t, sorted – t.unique() • Removes duplicate elements
  • 11. Functions of list class cntd- • list functions – t.swap(otherObject); • Exchange contents – t.assign(iterator1, iterator2) • Replaces contents with elements in range of iterators – t.remove(value) • Erases all instances of value
  • 12. List container int array[5] = {12, 7, 9, 21, 13 }; list<int> li(array,array+5); 7 9 21 12 li.push_front(8); 12 7 9 21 … 15 li.pop_front(); 12 9 21 13 li.push_back(15); 12 7 9 21 … 15 li.pop_back(); 8 7 12 17 21 23 li.insert() 19
  • 13. Functions of dequeue class • dequeue functions for object d -d.front() -Return a reference (or const_reference) to the first component of d -d.back() -Return a reference (or const_reference) to the last component of d. -d.size() -Return a value of type size_type giving the number of values currently in d.
  • 14. Functions of dequeue class contd- -d.push_back(val) -Add val to the end of d, increasing the size of d by one. -d.push_front(val) -Add val to the front of d, increasing the size of d by one. -d.pop_back() -Delete the last value of d. The size of d is reduced by one. -d.pop_front() -Delete the first value of d. The size of d is reduced by one.
  • 15. Associative Containers – Offer O(log n) insertion, suppression and access – Store only weakly strict ordered types (eg. numeric types) • Must have operator<() and operator==() defined and !(a<b) && !(b<a) (a==b)≡ – 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
  • 16. 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
  • 17. 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 Theiterator corresponding to the class vector<int> is of the typevector<int>::iterator
  • 18. Iterators contd- • The member functions begin() and end() return an iterator to the first and past the last element of a container vector<int> v array_ 17 4 23 12 size_ 4 v.end() v.begin()
  • 19. Iterators Categories • Not every iterator can be used with every container for example the list class provides no random access iterator • Every algorithm requires an iterator with a certain level of capability for example to use the [] operator you need a random access iterator • Iterators are divided into five categories in which a higher (more specific) category always subsumes a lower (more general) category, e.g. An algorithm that accepts a forward iterator will also work with a bidirectional iterator and a random access iterator input output forward bidirectional random access
  • 20. 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.
  • 21. 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
  • 22. 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;
  • 23. 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= {}
  • 24. Functions Objects • Some algorithms like sort, merge, accumulate can take a function object as argument. • A function object is an object of a template class that has a single member function : the overloaded operator () • It is also possible to use user-written functions in place of pre-defined function objects #include <list> #include <functional> int arr1[]= { 6, 4, 9, 1, 7 }; list<int> l1(arr1, arr1+5); // initialize l1 with arr1 l1.sort(greater<int>()); // uses function object greater<int> // for sorting in reverse order l1 = { 9, 7, 6, 4, 1 }
  • 25. Function Objects • The accumulate algorithm accumulates data over the elements of the containing, for example computing the sum of elements #include <list> #include <functional> #include <numeric> int arr1[]= { 6, 4, 9, 1, 7 }; list<int> l1(arr1, arr1+5); // initialize l1 with arr1 int sum = accumulate(l1.begin(), l1.end() , 0, plus<int>()); int sum = accumulate(l1.begin(), l1.end(),0); // equivalent int fac = accumulate(l1.begin(), l1.end() , 0, times<int>());
  • 26. User Defined Function Objects class squared _sum // user-defined function object { public: int operator()(int n1, int n2) { return n1+n2*n2; } }; int sq = accumulate(l1.begin(), l1.end() , 0, squared_sum() ); // computes the sum of squares
  • 27. THANK YOU.!!! So long and thanks for all the attention 
  • 28. ?