SlideShare ist ein Scribd-Unternehmen logo
1 von 23
STANDARD TEMPLATE
LIBRARIES
Michael Heron
Introduction
• The great promise of object orientation is reusability.
• As of now, we haven’t done much re-using.
• In the same way that Java provides comprehensive
libraries of basic structures, so too does C++
• Through the mechanism of the Standard Template Library.
Standard Template Library
• The STL is not a part of the core C++ language.
• It’s a set of extended libraries that have nearly
ubiquitous applicability.
• They are defined in the C++ standard.
• It provides implementations of many of the
standard data types available in other languages.
• It is driven by the power of templates (as
discussed in the previous lecture).
Standard Template Library
• The STL subset that we are going to pay most
attention to is that of collections.
• Basic, core data structures.
• You’ll hear more about how these are internally implemented in
a later part of the module.
• The collection classes allow us to make use of
powerful, flexible data types without us having to
write them ourselves.
• There’s a considerable development burden in writing
objects that are genuinely reuseable.
Collections
• Much like an array, a collection holds a number of discreet
elements.
• Unlike an array, ordering cannot be assumed with many
collections.
• These classes are not designed to be base classes for
more specialised derived classes.
• Destructors for example are explictly designed to be non-virtual.
Collections
• Collections break down into three main
categories.
• Sequential, where order is maintained.
• List
• Vector
• Associative, where no order is guaranteed.
• Set
• Hash Map
• Adapter, which act as wrappers around other core
structures.
• Queue
• Stack
The Vector
• The Vector is defined in the std namespace.
• #include <vector> into your code.
• Vectors are resizeable arrays.
• You can just keep adding things in and they’ll expand to meet your
requirements.
• Basic structure for interacting with a vector is common to
all STL collections.
The Vector
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> *v = new vector<int>();
v->push_back (100);
v->push_back (200);
v->push_back (500);
for (int i = 0; i < v->size(); i++) {
cout << v->at (i) << endl;
}
return 0;
}
The Vector
• Useful methods:
• push_back – push an element to the back of the structure
• size – get the number of elements in the collection
• at – pull the element out at that specific position.
• Memory management of a container done during
accesses.
Vector Memory Management
• Two measures for collections are available.
• capacity – how many elements can the collection hold before new
space needs to be allocated.
• max_size – how many elements, in total, the collection can hold.
• Determined by the system.
• resize() and reserve() allow for fine-grained control over
capacity.
The List
• The list implements a doubly-linked list for traversal.
• Can go forwards and backwards.
• Basic structure for adding to a list the same as with a
vector.
• Can also push_front
• Traversal of the list done through an iterator.
• Possible for most of the STL classes.
Iterators
• Iterators are a common interface across container
classes.
• They represent an object that allows traversal through a collection.
• Obtained by using the following syntax:
• Collection<type>::iterator
• list<int>::iterator
• The iterator serves as the basis for more elegant output of
state date.
Iterators
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> *l = new list<int>();
list<int>::iterator i;
l->push_back (100);
l->push_front(200);
for (i = l->begin(); i != l->end(); i++) {
cout << *i << endl;
}
return 0;
}
Reversal Traversal
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> *l = new list<int>();
list<int>::reverse_iterator i;
l->push_back (100);
l->push_front(200);
for (i = l->rbegin(); i != l->rend(); i++) {
cout << *i << endl;
}
return 0;
}
Collections
• Most sequence collections also implement a sort function.
• Another time we need to overload an operator as part of C++
• < operator must be overloaded to permit sorting of custom objects
based on developer requirements.
• Many other useful functions available.
• Won’t cover these in the lecture – documentation available in many
places.
Algorithms
• STL also contains implementations for common
algorithms.
• Sort
• Searches
• Must #include <algorithm> to get access to methods.
• Many of these methods similarly require operators to be
overloaded.
• Curse you C++.
Vector with Iterator, Sort and
Search
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> *v = new vector<int>();
vector<int>::iterator i;
bool found;
v->push_back (200);
v->push_back (100);
v->push_back (500);
sort (v->begin(), v->end());
found = binary_search (v->begin(), v->end(), 200);
cout << "Value 200 found in Vector" << endl;
for (i = v->begin(); i < v->end(); i++) {
cout << *i << endl;
}
return 0;
}
Class Defined for Sorting
class Person {
private:
int age;
public:
Person();
Person (int x);
void set_age (int x);
int query_age();
bool operator< (Person &a);
};
Class Implementation
#include "Person.h"
Person::Person() :
age (18) {
}
Person::Person (int x) :
age (x) {
}
int Person::query_age() {
return age;
}
void Person::set_age (int na) {
age = na;
}
bool operator<(Person& a, Person& b) {
return a.query_age() < a.query_age();
}
Versus Java
• It seems like much more is needed to implement a C++
class for collections than in Java.
• The same amount of code is required for both.
• Java requires an implementation of compareTo.
• C++ requires an implementation of an overloaded < operator.
Why Use STL Classes?
• The STL classes are battle-hardened and battle-scarred
• They work.
• They make use of templates, and thus can handle any
appropriately defined object you devise.
• Most of the common data structures are available as part
of the STL.
Why Not Use Them?
• Well, no real reason not to…
• … for real code.
• Array manipulation exercises and data structure
creation are important ‘clarity’ topics.
• It’s always worth understanding how data structures are
implemented.
• Remember, C++ is all about how things are represented
internally.
• Best to learn how to ‘roll your own’ first.
• Then use the STL versions because they are more
complete.
Summary
• C++ provides implementations of most of the standard
data types.
• And also corresponding implementations of default operations such
as searchs and sorts.
• Vectors and lists permit the traversal of ordered data.
• Important to define user classes appropriately for sorting
and searching.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

C++ STL 概觀
C++ STL 概觀C++ STL 概觀
C++ STL 概觀
 
An Introduction to the C++ Standard Library
An Introduction to the C++ Standard LibraryAn Introduction to the C++ Standard Library
An Introduction to the C++ Standard 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
 
List
ListList
List
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
 
Standard Library Functions
Standard Library FunctionsStandard Library Functions
Standard Library Functions
 
Sets
SetsSets
Sets
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
Algorithms: I
Algorithms: IAlgorithms: I
Algorithms: I
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
Dynamic memory allocation and linked lists
Dynamic memory allocation and linked listsDynamic memory allocation and linked lists
Dynamic memory allocation and linked lists
 
C++ arrays part1
C++ arrays part1C++ arrays part1
C++ arrays part1
 
Arrays
ArraysArrays
Arrays
 
Java10 Collections and Information
Java10 Collections and InformationJava10 Collections and Information
Java10 Collections and Information
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Array ppt
Array pptArray ppt
Array ppt
 
Tools for reading papers
Tools for reading papersTools for reading papers
Tools for reading papers
 
L11 array list
L11 array listL11 array list
L11 array list
 

Andere mochten auch

An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL乐群 陈
 
E rate presentation
E rate presentationE rate presentation
E rate presentationcoacheller
 
2CPP10 - Polymorphism
2CPP10 - Polymorphism2CPP10 - Polymorphism
2CPP10 - PolymorphismMichael Heron
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator OverloadingMichael Heron
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method OverridingMichael Heron
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and ClassesMichael Heron
 
2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation FundamentalsMichael Heron
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - InheritanceMichael Heron
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method OverloadingMichael Heron
 
2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented ProgramMichael Heron
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and PointersMichael Heron
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - AbstractionMichael Heron
 

Andere mochten auch (20)

Sysprog 10
Sysprog 10Sysprog 10
Sysprog 10
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
E rate presentation
E rate presentationE rate presentation
E rate presentation
 
2CPP19 - Summation
2CPP19 - Summation2CPP19 - Summation
2CPP19 - Summation
 
2CPP10 - Polymorphism
2CPP10 - Polymorphism2CPP10 - Polymorphism
2CPP10 - Polymorphism
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method Overriding
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
 
C++primer
C++primerC++primer
C++primer
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
 
2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - Inheritance
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
 
The STL
The STLThe STL
The STL
 
Sysprog 9
Sysprog 9Sysprog 9
Sysprog 9
 
2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 
2CPP02 - C++ Primer
2CPP02 - C++ Primer2CPP02 - C++ Primer
2CPP02 - C++ Primer
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 

Ähnlich wie 2CPP16 - STL

Ähnlich wie 2CPP16 - STL (20)

c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
c++ Unit III - PPT.pptx
 
UsingCPP_for_Artist.ppt
UsingCPP_for_Artist.pptUsingCPP_for_Artist.ppt
UsingCPP_for_Artist.ppt
 
Net framework
Net frameworkNet framework
Net framework
 
CSharp for Unity - Day 1
CSharp for Unity - Day 1CSharp for Unity - Day 1
CSharp for Unity - Day 1
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Learn c sharp at amc square learning
Learn c sharp at amc square learningLearn c sharp at amc square learning
Learn c sharp at amc square learning
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
Static abstract members nelle interfacce di C# 11 e dintorni di .NET 7.pptx
Static abstract members nelle interfacce di C# 11 e dintorni di .NET 7.pptxStatic abstract members nelle interfacce di C# 11 e dintorni di .NET 7.pptx
Static abstract members nelle interfacce di C# 11 e dintorni di .NET 7.pptx
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
DSA-Day-2-PS.pptx
DSA-Day-2-PS.pptxDSA-Day-2-PS.pptx
DSA-Day-2-PS.pptx
 
Intro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysIntro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & Arrays
 
Java Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O MechanismsJava Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O Mechanisms
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++
 

Mehr von Michael Heron

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMichael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconductMichael Heron
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkMichael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility SupportMichael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and AutershipMichael Heron
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interactionMichael Heron
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityMichael Heron
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - TexturesMichael Heron
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)Michael Heron
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)Michael Heron
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationMichael Heron
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsMichael Heron
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsMichael Heron
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationMichael Heron
 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - EncapsulationMichael Heron
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and OverridingMichael Heron
 

Mehr von Michael Heron (18)

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
 
SAD04 - Inheritance
SAD04 - InheritanceSAD04 - Inheritance
SAD04 - Inheritance
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - Shading
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical Representation
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - Encapsulation
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 

Kürzlich hochgeladen

AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...software pro Development
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 

Kürzlich hochgeladen (20)

AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 

2CPP16 - STL

  • 2. Introduction • The great promise of object orientation is reusability. • As of now, we haven’t done much re-using. • In the same way that Java provides comprehensive libraries of basic structures, so too does C++ • Through the mechanism of the Standard Template Library.
  • 3. Standard Template Library • The STL is not a part of the core C++ language. • It’s a set of extended libraries that have nearly ubiquitous applicability. • They are defined in the C++ standard. • It provides implementations of many of the standard data types available in other languages. • It is driven by the power of templates (as discussed in the previous lecture).
  • 4. Standard Template Library • The STL subset that we are going to pay most attention to is that of collections. • Basic, core data structures. • You’ll hear more about how these are internally implemented in a later part of the module. • The collection classes allow us to make use of powerful, flexible data types without us having to write them ourselves. • There’s a considerable development burden in writing objects that are genuinely reuseable.
  • 5. Collections • Much like an array, a collection holds a number of discreet elements. • Unlike an array, ordering cannot be assumed with many collections. • These classes are not designed to be base classes for more specialised derived classes. • Destructors for example are explictly designed to be non-virtual.
  • 6. Collections • Collections break down into three main categories. • Sequential, where order is maintained. • List • Vector • Associative, where no order is guaranteed. • Set • Hash Map • Adapter, which act as wrappers around other core structures. • Queue • Stack
  • 7. The Vector • The Vector is defined in the std namespace. • #include <vector> into your code. • Vectors are resizeable arrays. • You can just keep adding things in and they’ll expand to meet your requirements. • Basic structure for interacting with a vector is common to all STL collections.
  • 8. The Vector #include <iostream> #include <vector> using namespace std; int main() { vector<int> *v = new vector<int>(); v->push_back (100); v->push_back (200); v->push_back (500); for (int i = 0; i < v->size(); i++) { cout << v->at (i) << endl; } return 0; }
  • 9. The Vector • Useful methods: • push_back – push an element to the back of the structure • size – get the number of elements in the collection • at – pull the element out at that specific position. • Memory management of a container done during accesses.
  • 10. Vector Memory Management • Two measures for collections are available. • capacity – how many elements can the collection hold before new space needs to be allocated. • max_size – how many elements, in total, the collection can hold. • Determined by the system. • resize() and reserve() allow for fine-grained control over capacity.
  • 11. The List • The list implements a doubly-linked list for traversal. • Can go forwards and backwards. • Basic structure for adding to a list the same as with a vector. • Can also push_front • Traversal of the list done through an iterator. • Possible for most of the STL classes.
  • 12. Iterators • Iterators are a common interface across container classes. • They represent an object that allows traversal through a collection. • Obtained by using the following syntax: • Collection<type>::iterator • list<int>::iterator • The iterator serves as the basis for more elegant output of state date.
  • 13. Iterators #include <iostream> #include <list> using namespace std; int main() { list<int> *l = new list<int>(); list<int>::iterator i; l->push_back (100); l->push_front(200); for (i = l->begin(); i != l->end(); i++) { cout << *i << endl; } return 0; }
  • 14. Reversal Traversal #include <iostream> #include <list> using namespace std; int main() { list<int> *l = new list<int>(); list<int>::reverse_iterator i; l->push_back (100); l->push_front(200); for (i = l->rbegin(); i != l->rend(); i++) { cout << *i << endl; } return 0; }
  • 15. Collections • Most sequence collections also implement a sort function. • Another time we need to overload an operator as part of C++ • < operator must be overloaded to permit sorting of custom objects based on developer requirements. • Many other useful functions available. • Won’t cover these in the lecture – documentation available in many places.
  • 16. Algorithms • STL also contains implementations for common algorithms. • Sort • Searches • Must #include <algorithm> to get access to methods. • Many of these methods similarly require operators to be overloaded. • Curse you C++.
  • 17. Vector with Iterator, Sort and Search #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> *v = new vector<int>(); vector<int>::iterator i; bool found; v->push_back (200); v->push_back (100); v->push_back (500); sort (v->begin(), v->end()); found = binary_search (v->begin(), v->end(), 200); cout << "Value 200 found in Vector" << endl; for (i = v->begin(); i < v->end(); i++) { cout << *i << endl; } return 0; }
  • 18. Class Defined for Sorting class Person { private: int age; public: Person(); Person (int x); void set_age (int x); int query_age(); bool operator< (Person &a); };
  • 19. Class Implementation #include "Person.h" Person::Person() : age (18) { } Person::Person (int x) : age (x) { } int Person::query_age() { return age; } void Person::set_age (int na) { age = na; } bool operator<(Person& a, Person& b) { return a.query_age() < a.query_age(); }
  • 20. Versus Java • It seems like much more is needed to implement a C++ class for collections than in Java. • The same amount of code is required for both. • Java requires an implementation of compareTo. • C++ requires an implementation of an overloaded < operator.
  • 21. Why Use STL Classes? • The STL classes are battle-hardened and battle-scarred • They work. • They make use of templates, and thus can handle any appropriately defined object you devise. • Most of the common data structures are available as part of the STL.
  • 22. Why Not Use Them? • Well, no real reason not to… • … for real code. • Array manipulation exercises and data structure creation are important ‘clarity’ topics. • It’s always worth understanding how data structures are implemented. • Remember, C++ is all about how things are represented internally. • Best to learn how to ‘roll your own’ first. • Then use the STL versions because they are more complete.
  • 23. Summary • C++ provides implementations of most of the standard data types. • And also corresponding implementations of default operations such as searchs and sorts. • Vectors and lists permit the traversal of ordered data. • Important to define user classes appropriately for sorting and searching.