SlideShare a Scribd company logo
1 of 22
Download to read offline
C++ STL
Ms.M.Jancypriya,
Asst. Prof., Dept. of CA,
Bon Secours College for Women,
Thanjavur.
STL – Standard Template Library
• Collections of useful classes for common data structures
• Ability to store objects of any type (template)
• Study of containers
• Containers form the basis for treatment of data structures
• Container – class that stores a collection of data
• STL consists of 10 container classes:
• Sequence containers
• Adapter containers
• Associative containers
What is the STL?
• Standard Template Library
• A collection of classes to make C++ programming more
efficient
• Algorithms, Containers, and Iterators
• Function objects,Allocators, and Adapters
Containers Containers
Function Objects
Iterators
STLAlgorithms
• Accept STL Iterators as arguments
• Sort(begin, end);
• 4 categories
• Non-modifying
For each, Find, Count, Equal, Search, etc.
• Mutating
Copy, Generate, Reverse, Remove, Swap, etc.
• Sorting Related
Sort, Stable sort, Binary search, Merge, etc.
• Numeric
Accumulate, Inner product, Partial sum,
Adjacent difference
STL Containers
• Sequence Container
• Stores data by position in linear order:
• First element, second element , etc:
• Associate Container
• Stores elements by key, such as name, social security number or
part number
• Access an element by its key which may bear no relationship to the
location of the element in the container
• Adapter Container
• Contains another container as its underlying storage structure
STL Containers
 Sequence Container
 Vector
 Deque
 List
 Adapter Containers
 Stack
 Queue
 Priority queue
 Associative Container
 Set, multiset
 Map, multimap
Vector Container
• Generalized array that stores a collection of elements of
the same data type
• Vector – similar to an array
• Vectors allow access to its elements by using an index in the range
from 0 to n-1 where n is the size of the vector
• Vector vs array
• Vector has operations that allow the collection to grow and contract
dynamically at the rear of the sequence``
Vector Container
Example:
#include <vector>
.
.
.
vector<int> scores (100); //100 integer scores
vector<Passenger>passengerList(20); //list of 20
passengers
Vector Container
• Allows direct access to the elements via an index operator
• Indices for the vector elements are in the range from 0 to
size() -1
• Example:
#include <vector>
vector <int> v(20);
v[5]=15;
List Container
• Stores elements by position
• Each item in the list has both a value and a memory
address (pointer) that identifies the next item in the
sequence
• To access a specific data value in the list, one must start
at the first position (front) and follow the pointers from
element to element until data item is located.
• List is not a direct access structure
• Advantage: ability to add and remove items efficiently at
any position in the sequence
Stack Container
• Adapter Container
• These containers restrict how elements enter and leave a
sequence
• Stack
• allows access at only one end of the sequence (top)
• Adds objects to container by pushing the object onto the stack
• Removes objects from container by popping the stack
• LIFO ordering (last end, first out)
Queue Container
• Queue
• Allows access only at the front and rear of the sequence
• Items enter at the rear and exit from the front
• Example: waiting line at a grocery store
• FIFO ordering (first-in first-out )
• push(add object to a queue)
• pop (remove object from queue)
Priority Queue Container
• Priority queue
• Operations are similar to those of a stack or queue
• Elements can enter the priority queue in any order
• Once in the container, a delete operation removes the largest (or
smallest) value
• Example: a filtering system that takes in elements and then
releases them in priority order 8
18 13
3 15
27
Set Container
• Set
• Collection of unique values, called keys or set members
• Contains operations that allow a programmer to:
• determine whether an item is a member of the set
• insert and delete items very efficiently
5 1 3
6 27 15
Buick
Ford
Jeep BMW
Set A Set B
Multi-Set Container
• A multi-set is similar to a set, but the same value can be in
the set more than once
• Multi-set container allows duplicates
Map Container
• Implements a key-value relationship
• Programmer can use a key to access corresponding
values
• Example: key could be a part number such as A24-57
that corresponds to a part: 8.75 price and Martin
manufacturer
A22-56
A23-57
A24-57
A24-57 8.75 Martin
A22-56 12.50 Callowa
y
A23-57 4.95 Mirage
Multi-map Container
• Similar to a map container
• Multi-map container allows duplicates
How to access Components - Iterator
• Iterator is an object that can access a collection of like
objects one object at a time.
• An iterator can traverse the collection of objects.
• Each container class in STL has a corresponding iterator
that functions appropriately for the container
• For example: an iterator in a vector class allows random
access
• An iterator in a list class would not allow random access
(list requires sequential access)
Common Iterator Operations
* Return the item that the iterator currently references
++ Move the iterator to the next item in the list
-- Move the iterator to the previous item in the
list
== Compare two iterators for equality
!= Compare two iterators for inequality
STL List Class
Constructors and assignment
• list <T> v;
• list<T> v(aList);
• l=aList;
Access
• l.front() ----returns 1st element in the list
• l.back()----returns the last element in the list
STL List
Insert and Remove
• l.push_front(value)
• l.push_back(value)
Iterator Delaration
• list<T>::iteratoritr;
Iterator Options
• itr = l.begin() set iterator to beginning of the list
• Itr = l.end() set iterator to after the end of the list
Writing classes that work with the STL
 Classes that will be stored in STL containers should
explicitly define the following:
◦ Default constructor
◦ Copy constructor
◦ Destructor
◦ operator =
◦ operator==
◦ operator<
 Not all of these are always necessary, but it might be
easier to define them than to figure out which ones
you actually need
 Many STL programming errors can be traced to
omitting or improperly defining these methods

More Related Content

What's hot

What's hot (20)

Stack project
Stack projectStack project
Stack project
 
Function C++
Function C++ Function C++
Function C++
 
Python array
Python arrayPython array
Python array
 
List in java
List in javaList in java
List in java
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work ppt
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 
Queues
QueuesQueues
Queues
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 

Similar to Standard template library

How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)Sangharsh agarwal
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptxASRPANDEY
 
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
 
Python programming
Python programmingPython programming
Python programmingsirikeshava
 
Data structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxData structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxAhmedEldesoky24
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collectionPrem Kumar Badri
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for publiciqbalphy1
 
Acm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAcm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAhmad Bashar Eter
 
Stl Containers
Stl ContainersStl Containers
Stl Containersppd1961
 
ArrayList class and useful methods.pptx
ArrayList class and useful methods.pptxArrayList class and useful methods.pptx
ArrayList class and useful methods.pptxAbid523408
 

Similar to Standard template library (20)

How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)
 
oops_final_ppt[1].pptx
oops_final_ppt[1].pptxoops_final_ppt[1].pptx
oops_final_ppt[1].pptx
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptx
 
Queues
Queues Queues
Queues
 
Collections Training
Collections TrainingCollections Training
Collections Training
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
Python programming
Python programmingPython programming
Python programming
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Data structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxData structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptx
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collection
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
Queue
QueueQueue
Queue
 
Acm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAcm aleppo cpc training seventh session
Acm aleppo cpc training seventh session
 
Javasession7
Javasession7Javasession7
Javasession7
 
Stl Containers
Stl ContainersStl Containers
Stl Containers
 
The Standard Template Library
The Standard Template LibraryThe Standard Template Library
The Standard Template Library
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
ArrayList class and useful methods.pptx
ArrayList class and useful methods.pptxArrayList class and useful methods.pptx
ArrayList class and useful methods.pptx
 

More from Jancypriya M

Introduction to computer
Introduction to computerIntroduction to computer
Introduction to computerJancypriya M
 
Library automation software
Library automation softwareLibrary automation software
Library automation softwareJancypriya M
 
Flip Flop and Its Types
Flip Flop and Its TypesFlip Flop and Its Types
Flip Flop and Its TypesJancypriya M
 
Introduction to pagemaker
Introduction to pagemakerIntroduction to pagemaker
Introduction to pagemakerJancypriya M
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating stringsJancypriya M
 
Manipulation of Strings
Manipulation of StringsManipulation of Strings
Manipulation of StringsJancypriya M
 

More from Jancypriya M (10)

Introduction to computer
Introduction to computerIntroduction to computer
Introduction to computer
 
Library automation software
Library automation softwareLibrary automation software
Library automation software
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Flip Flop and Its Types
Flip Flop and Its TypesFlip Flop and Its Types
Flip Flop and Its Types
 
Corel draw tools
Corel draw toolsCorel draw tools
Corel draw tools
 
Applet in java
Applet in javaApplet in java
Applet in java
 
Introduction to pagemaker
Introduction to pagemakerIntroduction to pagemaker
Introduction to pagemaker
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
 
Strings
StringsStrings
Strings
 
Manipulation of Strings
Manipulation of StringsManipulation of Strings
Manipulation of Strings
 

Recently uploaded

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 

Recently uploaded (20)

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 

Standard template library

  • 1. C++ STL Ms.M.Jancypriya, Asst. Prof., Dept. of CA, Bon Secours College for Women, Thanjavur.
  • 2. STL – Standard Template Library • Collections of useful classes for common data structures • Ability to store objects of any type (template) • Study of containers • Containers form the basis for treatment of data structures • Container – class that stores a collection of data • STL consists of 10 container classes: • Sequence containers • Adapter containers • Associative containers
  • 3. What is the STL? • Standard Template Library • A collection of classes to make C++ programming more efficient • Algorithms, Containers, and Iterators • Function objects,Allocators, and Adapters Containers Containers Function Objects Iterators
  • 4. STLAlgorithms • Accept STL Iterators as arguments • Sort(begin, end); • 4 categories • Non-modifying For each, Find, Count, Equal, Search, etc. • Mutating Copy, Generate, Reverse, Remove, Swap, etc. • Sorting Related Sort, Stable sort, Binary search, Merge, etc. • Numeric Accumulate, Inner product, Partial sum, Adjacent difference
  • 5. STL Containers • Sequence Container • Stores data by position in linear order: • First element, second element , etc: • Associate Container • Stores elements by key, such as name, social security number or part number • Access an element by its key which may bear no relationship to the location of the element in the container • Adapter Container • Contains another container as its underlying storage structure
  • 6. STL Containers  Sequence Container  Vector  Deque  List  Adapter Containers  Stack  Queue  Priority queue  Associative Container  Set, multiset  Map, multimap
  • 7. Vector Container • Generalized array that stores a collection of elements of the same data type • Vector – similar to an array • Vectors allow access to its elements by using an index in the range from 0 to n-1 where n is the size of the vector • Vector vs array • Vector has operations that allow the collection to grow and contract dynamically at the rear of the sequence``
  • 8. Vector Container Example: #include <vector> . . . vector<int> scores (100); //100 integer scores vector<Passenger>passengerList(20); //list of 20 passengers
  • 9. Vector Container • Allows direct access to the elements via an index operator • Indices for the vector elements are in the range from 0 to size() -1 • Example: #include <vector> vector <int> v(20); v[5]=15;
  • 10. List Container • Stores elements by position • Each item in the list has both a value and a memory address (pointer) that identifies the next item in the sequence • To access a specific data value in the list, one must start at the first position (front) and follow the pointers from element to element until data item is located. • List is not a direct access structure • Advantage: ability to add and remove items efficiently at any position in the sequence
  • 11. Stack Container • Adapter Container • These containers restrict how elements enter and leave a sequence • Stack • allows access at only one end of the sequence (top) • Adds objects to container by pushing the object onto the stack • Removes objects from container by popping the stack • LIFO ordering (last end, first out)
  • 12. Queue Container • Queue • Allows access only at the front and rear of the sequence • Items enter at the rear and exit from the front • Example: waiting line at a grocery store • FIFO ordering (first-in first-out ) • push(add object to a queue) • pop (remove object from queue)
  • 13. Priority Queue Container • Priority queue • Operations are similar to those of a stack or queue • Elements can enter the priority queue in any order • Once in the container, a delete operation removes the largest (or smallest) value • Example: a filtering system that takes in elements and then releases them in priority order 8 18 13 3 15 27
  • 14. Set Container • Set • Collection of unique values, called keys or set members • Contains operations that allow a programmer to: • determine whether an item is a member of the set • insert and delete items very efficiently 5 1 3 6 27 15 Buick Ford Jeep BMW Set A Set B
  • 15. Multi-Set Container • A multi-set is similar to a set, but the same value can be in the set more than once • Multi-set container allows duplicates
  • 16. Map Container • Implements a key-value relationship • Programmer can use a key to access corresponding values • Example: key could be a part number such as A24-57 that corresponds to a part: 8.75 price and Martin manufacturer A22-56 A23-57 A24-57 A24-57 8.75 Martin A22-56 12.50 Callowa y A23-57 4.95 Mirage
  • 17. Multi-map Container • Similar to a map container • Multi-map container allows duplicates
  • 18. How to access Components - Iterator • Iterator is an object that can access a collection of like objects one object at a time. • An iterator can traverse the collection of objects. • Each container class in STL has a corresponding iterator that functions appropriately for the container • For example: an iterator in a vector class allows random access • An iterator in a list class would not allow random access (list requires sequential access)
  • 19. Common Iterator Operations * Return the item that the iterator currently references ++ Move the iterator to the next item in the list -- Move the iterator to the previous item in the list == Compare two iterators for equality != Compare two iterators for inequality
  • 20. STL List Class Constructors and assignment • list <T> v; • list<T> v(aList); • l=aList; Access • l.front() ----returns 1st element in the list • l.back()----returns the last element in the list
  • 21. STL List Insert and Remove • l.push_front(value) • l.push_back(value) Iterator Delaration • list<T>::iteratoritr; Iterator Options • itr = l.begin() set iterator to beginning of the list • Itr = l.end() set iterator to after the end of the list
  • 22. Writing classes that work with the STL  Classes that will be stored in STL containers should explicitly define the following: ◦ Default constructor ◦ Copy constructor ◦ Destructor ◦ operator = ◦ operator== ◦ operator<  Not all of these are always necessary, but it might be easier to define them than to figure out which ones you actually need  Many STL programming errors can be traced to omitting or improperly defining these methods