SlideShare a Scribd company logo
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.

More Related Content

What's hot

C++ STL 概觀
C++ STL 概觀C++ STL 概觀
C++ STL 概觀
PingLun Liao
 
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
Joyjit Choudhury
 
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 Library
Kumar Gaurav
 
List
ListList
Priority Queue
Priority QueuePriority Queue
Priority Queue
Joyjit Choudhury
 
Standard Library Functions
Standard Library FunctionsStandard Library Functions
Standard Library Functions
Praveen M Jigajinni
 
Sets
SetsSets
Algorithms: I
Algorithms: IAlgorithms: I
Algorithms: I
Joyjit Choudhury
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
Joyjit Choudhury
 
Dynamic memory allocation and linked lists
Dynamic memory allocation and linked listsDynamic memory allocation and linked lists
Dynamic memory allocation and linked lists
Deepam Aggarwal
 
C++ arrays part1
C++ arrays part1C++ arrays part1
C++ arrays part1
Subhasis Nayak
 
Arrays
ArraysArrays
Java10 Collections and Information
Java10 Collections and InformationJava10 Collections and Information
Java10 Collections and Information
SoftNutx
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Array ppt
Array pptArray ppt
Array ppt
Kaushal Mehta
 
Tools for reading papers
Tools for reading papersTools for reading papers
Tools for reading papers
Jack Fox
 
L11 array list
L11 array listL11 array list
L11 array list
teach4uin
 

What's hot (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
 

Viewers also liked

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
 
2CPP19 - Summation
2CPP19 - Summation2CPP19 - Summation
2CPP19 - Summation
Michael Heron
 
2CPP10 - Polymorphism
2CPP10 - Polymorphism2CPP10 - Polymorphism
2CPP10 - Polymorphism
Michael Heron
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
Michael Heron
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method Overriding
Michael Heron
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
Michael Heron
 
C++primer
C++primerC++primer
C++primer
leonlongli
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
Michael Heron
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
Michael Heron
 
2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals
Michael Heron
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - Inheritance
Michael Heron
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
Michael Heron
 
The STL
The STLThe STL
The STL
adil raja
 
2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program
Michael Heron
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
Michael Heron
 
2CPP02 - C++ Primer
2CPP02 - C++ Primer2CPP02 - C++ Primer
2CPP02 - C++ Primer
Michael Heron
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
Michael Heron
 

Viewers also liked (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
 

Similar to 2CPP16 - STL

c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
UsingCPP_for_Artist.ppt
UsingCPP_for_Artist.pptUsingCPP_for_Artist.ppt
UsingCPP_for_Artist.ppt
vinu28455
 
Net framework
Net frameworkNet framework
Net framework
Abhishek Mukherjee
 
CSharp for Unity - Day 1
CSharp for Unity - Day 1CSharp for Unity - Day 1
CSharp for Unity - Day 1
Duong Thanh
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
ssuser0c24d5
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
nilesh405711
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
YashpalYadav46
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
DevliNeeraj
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
Steve Johnson
 
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
ASIT Education
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
Michael Heron
 
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
Marco Parenzan
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 
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
amanbhogal7
 
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
Blue Elephant Consulting
 
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
Subhadra Sundar Chakraborty
 
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++
Khushal Mehta
 

Similar to 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++
 

More from Michael Heron

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
Michael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
Michael Heron
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
Michael 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 Support
Michael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
Michael Heron
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
Michael Heron
 
SAD04 - Inheritance
SAD04 - InheritanceSAD04 - Inheritance
SAD04 - Inheritance
Michael Heron
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
Michael Heron
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
Michael Heron
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - Shading
Michael 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 Representation
Michael Heron
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
Michael Heron
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
Michael Heron
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
Michael Heron
 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - Encapsulation
Michael Heron
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
Michael Heron
 

More from 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
 

Recently uploaded

Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 

Recently uploaded (20)

Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 

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.