Module 202122 The Standard Template Library In this
Module 20/21/22: The Standard Template Library In this module we will cover • The basic STL containers -vector -list [-map] • Iteratorsp • STL algorithms Scientific Computing in OO Module 20/21/22: The STL #1 Course code 3 C 59 2000/01
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. Scientific Computing in OO Module 20/21/22: The STL #2 Course code 3 C 59 2000/01
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. #include <vector. h> #include <string. h> #include “Particle. h” A vector is a collection of objects std: : vector <float> my. Vector; It holds those objects for you std: : vector <string> Guest. List; It is declared in a slight peculiar way with a new sort of brackets std: : vector <int> Lottery. Numbers; std: : vector <Particle> particle. List; < > which tell the compiler what sort of object you are going to store in this vector Scientific Computing in OO Module 20/21/22: The STL #3 Course code 3 C 59 2000/01
#include <vector. h> #include <string. h> #include “Particle. hpp” vector <Particle> is a class so particle. List is an object std: : vector <float> my. Vector; so it has methods you can invoke with particle. List. method. Name( ) just like any other object std: : vector <string> Guest. List; std: : vector <int> Lottery. Numbers; std: : vector <Particle> particle. List; particle. List is an object Scientific Computing in OO Module 20/21/22: The STL #4 Course code 3 C 59 2000/01
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> particle. List; 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> particle. List(. . . . ) ; Scientific Computing in OO Module 20/21/22: The STL #5 Course code 3 C 59 2000/01
We have seen the use of some vector methods already #include <vector. h> #include <string. h> std: : vector <string> name. List; . . . . std: : string s = “Hello” name. List. push_back( s ); . . . . int n = namelist. size() ; Here are some more: Scientific Computing in OO Module 20/21/22: The STL #6 Course code 3 C 59 2000/01
push_back() and pop_back( ) #include <vector. h> #include “Particle. hpp”. . . std: : vector <Particle> particle. List; . . . // Push a particle onto the vector Particle p; particle. List. push_back( p ); //Remove last entry from vector particle. List. pop_back(); Scientific Computing in OO Module 20/21/22: The STL #7 Course code 3 C 59 2000/01
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> data. List; . . . // some code // Print out all integers for (int index = 0; index < data. List. size(); index++) { int item ; item = data. List. at(index) ; std: : cout << item << std: : endl; } Scientific Computing in OO Module 20/21/22: The STL #8 Course code 3 C 59 2000/01
front( ) and back( ) To get at the item at the front or back of the vector #include <vector. h> std: : vector <Particle> data. List; . . . // some code // Get item at front Particle f; f = data. List. front() ; // Get item at back Particle b; b = data. List. back() ; Scientific Computing in OO Module 20/21/22: The STL #9 Course code 3 C 59 2000/01
clear( ) To clear a vector of all contents #include <vector. h> std: : vector <Particle> data. List; . . . // some code // clear the vector data. List. clear() ; Scientific Computing in OO Module 20/21/22: The STL #10 Course code 3 C 59 2000/01
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> incoming. Particles); }; return type Scientific Computing in OO Module 20/21/22: The STL argument type #11 Course code 3 C 59 2000/01
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> particle. List; . . . // Push a particle Particle p; particle. List. push_back( p ); //Remove last entry particle. List. pop_back(); // Find size int n = particle. List. size() ; Scientific Computing in OO Module 20/21/22: The STL #12 Course code 3 C 59 2000/01
• 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> mailing. List; . . . string my. Address(“ 666 West Wallaby Street”); mailing. List. push_front(my. Address); Scientific Computing in OO Module 20/21/22: The STL #13 Course code 3 C 59 2000/01
21. 1 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”. Scientific Computing in OO Module 20/21/22: The STL #14 Course code 3 C 59 2000/01
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 particle. Iterator particle. Store Scientific Computing in OO Module 20/21/22: The STL #15 Course code 3 C 59 2000/01
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 particle. Iterator implements the ++ operator so that after particle. Iterator++ it points to next object particle. Store Scientific Computing in OO Module 20/21/22: The STL #16 Course code 3 C 59 2000/01
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 particle. Iterator float m = particle. Iterator->mass ; particle. Store Scientific Computing in OO Module 20/21/22: The STL #17 Course code 3 C 59 2000/01
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. Create an iterator suitable for iterating over a list #include <list> #include “Address. Label. hpp” // a list of Adress. Labels list <Address. Label> mailing. List; //an iterator which works on lists //of Address. Labels list <Address. Label>: : iterator iter; //this is how you use the iterator for (iter = mailing. List. begin() ; iter != mailing. List. end() ; iter++) { iter->print(); } Scientific Computing in OO Module 20/21/22: The STL #18 Course code 3 C 59 2000/01
iter is set to the start of mailing. List #include <list> #include “Address. Label. hpp” // a list of Adress. Labels list <Address. Label> mailing. List; //an iterator which works on lists //of Address. Labels list <Address. Label>: : iterator iter; //this is how you use the iterator for (iter = mailing. List. begin() ; iter != mailing. List. end() ; iter++) { iter->print(); } Scientific Computing in OO Module 20/21/22: The STL #19 Course code 3 C 59 2000/01
#include <list> #include “Particle. h” // a list of Adress. Labels list <Particle> particle. List; //an iterator which works on lists //of Address. Labels list <Address. Label>: : iterator iter; loop end condition: stop when iter is at the end of mailing. List //this is how you use the iterator for (iter = mailing. List. begin() ; iter != mailing. List. end() ; iter++) { iter->print(); } Scientific Computing in OO Module 20/21/22: The STL #20 Course code 3 C 59 2000/01
#include <list> #include “Address. Label. hpp” // a list of Adress. Labels list <Address. Label> mailing. List; steps through the list one at a time with ++ //an iterator which works on lists //of Address. Labels list <Address. Label>: : iterator iter; //this is how you use the iterator for (iter = mailing. List. begin() ; iter != mailing. List. end() ; iter++) { iter->print(); } Scientific Computing in OO Module 20/21/22: The STL #21 Course code 3 C 59 2000/01
#include <list> #include “Address. Label. hpp” // a list of Adress. Labels list <Address. Label> mailing. List; //an iterator which works on lists //of Address. Labels list <Address. Label>: : iterator iter; This is how you access the members of the list- this calls the print( ) method of each Address. Label in the list in turn. iter is like a pointer to an Address. Label so we use -> //this is how you use the iterator for (iter = mailing. List. begin() ; iter != mailing. List. end() ; iter++) { iter->print(); } Scientific Computing in OO Module 20/21/22: The STL #22 Course code 3 C 59 2000/01
Iterators aren’t just for lists They work for vectors too. . . vector <Particle> particle. List; vector <Particle>: : iterator another. Iterator; for (another. Iterator = particle. List. begin() ; another. Iterator != particle. List. end(); another. Iterator++) { // some code } 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 Scientific Computing in OO Module 20/21/22: The STL #23 Course code 3 C 59 2000/01
21. 2 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 sorts entries into order 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 Scientific Computing in OO Module 20/21/22: The STL #24 Course code 3 C 59 2000/01
21. 3 The sort algorithm for primitives #include <vector> #include <algorithm> vector <float> class. Height. List; . . . vector<float>: : iterator start ; vector<float>: : iterator finish ; start = class. Height. List. begin() ; finish = class. Height. List. end() ; sort( start, finish ); This simple line will have sorted the entire vector for you Scientific Computing in OO Module 20/21/22: The STL #25 Course code 3 C 59 2000/01
#include <vector> #include <algorithm> list <float> class. Height. List; . . . list<float>: : iterator start ; list<float>: : iterator finish ; start = class. Height. List. begin() ; finish = class. Height. List. end() ; sort( start, finish ); . . or with no change it will sort a list Scientific Computing in OO Module 20/21/22: The STL #26 Course code 3 C 59 2000/01
21. 4 The sort algorithm for objects #include <vector> #include <algorithm> vector <Three. Vector> tracks; . . . vector<Three. Vector>: : iterator start ; vector<Three. Vector>: : 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 Scientific Computing in OO Module 20/21/22: The STL #27 Course code 3 C 59 2000/01
#include <vector> #include <algorithm> vector <Three. Vector> tracks; . . . vector<Three. Vector>: : iterator start ; vector<Three. Vector>: : 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 Scientific Computing in OO Module 20/21/22: The STL #28 Course code 3 C 59 2000/01
. . provided that you have told the compiler one very simple bit of information, which is how to compare two Three. Vectors 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 Three. Vector class, i. e. bool Three. Vector: : operator<( Three. Vector& 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. Scientific Computing in OO Module 20/21/22: The STL #29 Course code 3 C 59 2000/01
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 ! Scientific Computing in OO Module 20/21/22: The STL #30 Course code 3 C 59 2000/01
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. . Scientific Computing in OO Module 20/21/22: The STL #31 Course code 3 C 59 2000/01
- Slides: 31