Download presentation
Presentation is loading. Please wait.
Published byAlexandrina Wheeler Modified over 7 years ago
1
Module 20/21/22: The Standard Template Library
In this module we will cover The basic STL containers -vector -list [-map] Iteratorsp STL algorithms
2
Aims of this module Every large program deals not with single objects but with COLLECTIONS of objects. C++ has a powerful set of collections in the Standard Template Library (STL) which we can use to hold collections of objects and to loop or ITERATE over them. These can do a lot of the work for you. This module introduces the vector, list and map collections and shows how to use them. It also introduces the concept of an STL iterator and the generic algorithms which can be applied to STL collections.
3
20.1 The vector<....> class
We were introduced to the vector class in an earlier module where we covered the minimal hands on knowledge needed to use it. A vector is a collection of objects It holds those objects for you It is declared in a slight peculiar way with a new sort of brackets < > which tell the compiler what sort of object you are going to store in this vector #include <vector.h> #include <string.h> #include “Particle.h” std::vector <float> myVector; std::vector <string> GuestList; std::vector <int> LotteryNumbers; std::vector <Particle> particleList;
4
particleList is an object
#include <vector.h> #include <string.h> #include “Particle.hpp” std::vector <float> myVector; std::vector <string> GuestList; std::vector <int> LotteryNumbers; std::vector <Particle> particleList; vector <Particle> is a class so particleList is an object so it has methods you can invoke with particleList.methodName( ) just like any other object particleList is an object
5
vector <Particle >
Since this is a class it has constructors too Here we are invoking its default constructor #include <vector.h> #include “Particle.hpp” // this is invoking the // default constructor std::vector <Particle> particleList; There are other constructors which we would invoke like this: #include <vector.h> #include “Particle.hpp” // this is invoking the // default constructor std::vector <Particle> particleList( ) ;
6
We have seen the use of some vector methods already
#include <vector.h> #include <string.h> std::vector <string> nameList; .... std::string s = “Hello” nameList.push_back( s ); int n = namelist.size() ; Here are some more:
7
push_back() and pop_back( )
#include <vector.h> #include “Particle.hpp” ... std::vector <Particle> particleList; // Push a particle onto the vector Particle p; particleList.push_back( p ); //Remove last entry from vector particleList.pop_back();
8
at( ) When you need to get things back from the vector, you can retrieve the elements using the at( ) method As always, C++ counts from 0 to n-1 #include <vector.h> std::vector <int> dataList; ... // some code // Print out all integers for (int index = 0; index < dataList.size();index++) { int item ; item = dataList.at(index) ; std::cout << item << std::endl; }
9
front( ) and back( ) To get at the item at the front or back of the vector #include <vector.h> std::vector <Particle> dataList; ... // some code // Get item at front Particle f; f = dataList.front() ; // Get item at back Particle b; b = dataList.back() ;
10
clear( ) To clear a vector of all contents #include <vector.h>
std::vector <Particle> dataList; ... // some code // clear the vector dataList.clear() ;
11
20.2 vectors in function or method signatures
Remember - vector <Particle> is a class you make objects of type vector <Particle> This means you can use them in function arguments and returns #include <vector> #include “Particle.h” class Scatterer { ... vector<Particle> scatter ( vector<Particle> incomingParticles); }; return type argument type type
12
20.3 The list<....> class
vector is only one of the containers in the STL lists are similar to vectors but are more efficient if you want to insert things anywhere in the list. list<...> methods are for the large part the same as vector<....> methods #include <vector.h> #include “Particle.hpp” ... std::list <Particle> particleList; // Push a particle Particle p; particleList.push_back( p ); //Remove last entry particleList.pop_back(); // Find size int n = particleList.size() ;
13
list<...> DOES NOT implement the [ ] operator.
You can add or remove the front item of a list with push_front( ) and pop_front( ) (that doesn’t work with vector) #include <list> #include <string> ... list<string> mailingList; string myAddress(“666 West Wallaby Street”); mailingList.push_front(myAddress);
14
Iterators Clever people (not me) thought about the concepts involved in maintaining collections of items, and passing through them. They abstracted this from the actual storage mechanism itself. This led to the concept of an “iterator” An iterator is a clever object, designed to let you step through any collection of items in a uniform way, no matter what he underlying storage mechanism. Iterators obey a set of “rules”.
15
vector<Particle> vector<particle>::iterator
Rule 1: At any one time an iterator “points” to one object in the collection object of type: vector<Particle> object of type: vector<particle>::iterator At any time, it maintains an internal pointer (or some sort) to an object in the collection particleIterator particleStore
16
vector<Particle>
Rule 2: an iterator implements the ++ operator to move on to the next item object of type: vector<Particle> object of type: vector<particle>::iterator particleIterator implements the ++ operator so that after particleIterator++ it points to next object particleStore
17
vector<Particle> vector<particle>::iterator
Rule 3: an iterator implements the -> operator to give you the item in the collection object of type: vector<Particle> object of type: vector<particle>::iterator particleIterator float m = particleIterator->mass ; particleStore
18
Create an iterator suitable for iterating over a list
Here is the completely standard way in which iterators are used. If you can follow this slide then you know all you need to know for most things. #include <list> #include “AddressLabel.hpp” // a list of AdressLabels list <AddressLabel> mailingList; //an iterator which works on lists //of AddressLabels list <AddressLabel>::iterator iter; //this is how you use the iterator for (iter = mailingList.begin() ; iter != mailingList.end() ; iter++) { iter->print(); } Create an iterator suitable for iterating over a list
19
iter is set to the start of
#include <list> #include “AddressLabel.hpp” // a list of AdressLabels list <AddressLabel> mailingList; //an iterator which works on lists //of AddressLabels list <AddressLabel>::iterator iter; //this is how you use the iterator for (iter = mailingList.begin() ; iter != mailingList.end() ; iter++) { iter->print(); } iter is set to the start of mailingList
20
loop end condition: stop when
#include <list> #include “Particle.h” // a list of AdressLabels list <Particle> particleList; //an iterator which works on lists //of AddressLabels list <AddressLabel>::iterator iter; //this is how you use the iterator for (iter = mailingList.begin() ; iter != mailingList.end() ; iter++) { iter->print(); } loop end condition: stop when iter is at the end of mailingList
21
steps through the list one at a time with ++
#include <list> #include “AddressLabel.hpp” // a list of AdressLabels list <AddressLabel> mailingList; //an iterator which works on lists //of AddressLabels list <AddressLabel>::iterator iter; //this is how you use the iterator for (iter = mailingList.begin() ; iter != mailingList.end() ; iter++) { iter->print(); } steps through the list one at a time with ++
22
#include <list>
#include “AddressLabel.hpp” // a list of AdressLabels list <AddressLabel> mailingList; //an iterator which works on lists //of AddressLabels list <AddressLabel>::iterator iter; //this is how you use the iterator for (iter = mailingList.begin() ; iter != mailingList.end() ; iter++) { iter->print(); } This is how you access the members of the list- this calls the print( ) method of each AddressLabel in the list in turn. iter is like a pointer to an AddressLabel so we use ->
23
Iterators aren’t just for lists
They work for vectors too... Why on earth would you bother with iterators and lists when you have vectors and [ ]? For most scientific applications, you don’t have to: vectors are powerful and very useful But sometimes the other collections can be useful And the way they are written, with iterators, allows you to use powerful generic algorithms on any sort of STL container-as we’ll see later vector <Particle> particleList; vector <Particle>::iterator anotherIterator; for (anotherIterator = particleList.begin() ; anotherIterator != particleList.end(); anotherIterator++) { // some code }
24
GENERIC ALGORITHMS Because all the STL containers work with iterators, you can write algorithms that don’t care what sort of container you give them The algorithms will work on containers of any kind of object (so long as the appropriate operators are defined) For example: accumulate sums all the entries in the container min_element returns minimum value sort sorts entries into order find finds an element in the container and tells you where it lies copy copies (all or parts of) one container to another- even to different types of container
25
This simple line will have sorted the entire vector for you
The sort algorithm for primitives #include <vector> #include <algorithm> vector <float> classHeightList; ... vector<float>::iterator start ; vector<float>::iterator finish ; start = classHeightList.begin() ; finish = classHeightList.end() ; sort( start, finish ); This simple line will have sorted the entire vector for you
26
.... or with no change it will sort a list
#include <vector> #include <algorithm> list <float> classHeightList; ... list<float>::iterator start ; list<float>::iterator finish ; start = classHeightList.begin() ; finish = classHeightList.end() ; sort( start, finish ); .... or with no change it will sort a list
27
This simple line will have sorted the entire vector of tracks for you
The sort algorithm for objects #include <vector> #include <algorithm> vector <ThreeVector> tracks; ... vector<ThreeVector>::iterator start ; vector<ThreeVector>::iterator finish ; start = tracks.begin() ; finish = tracks.end() ; sort( start, finish ); This simple line will have sorted the entire vector of tracks for you provided that p.t.o
28
This simple line will have sorted the entire vector of tracks for you
#include <vector> #include <algorithm> vector <ThreeVector> tracks; ... vector<ThreeVector>::iterator start ; vector<ThreeVector>::iterator finish ; start = tracks.begin() ; finish = tracks.end() ; sort( start, finish ); This simple line will have sorted the entire vector of tracks for you provided that p.t.o
29
Ordering means being able to answer the question: if( a < b )
.... provided that you have told the compiler one very simple bit of information, which is how to compare two ThreeVectors for ordering Ordering means being able to answer the question: if( a < b ) for any two objects a and b This is achieved by defining the < operator for the ThreeVector class,i.e. bool ThreeVector::operator<( ThreeVector& rhs ) { if( this->magnitude() < rhs.magnitude() ) return true ; else return false ; } This is all you have to write – after that the pre-written sort algorithm will work for you for free.
30
This all works because of the feature known as
TEMPLATES in C++ Unfortunately, due to time constraints, there is not time this year to teach templates. I will show anyone who wants to know privately – preferably while you are buying me a beer !
31
The Standard Template Library
Summary of Module 16: The Standard Template Library In this module we have covered: The basic STL containers vector list map Iterators begin( ), end( ), typedef of iterators, iter++ Generic Algorithms sort, find, etc..
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.