Professional Documents
Culture Documents
CO1401 Week 9 Lecture
CO1401 Week 9 Lecture
Programming
Week 9
STL & Vector Class
The module
• The module has gone through all of the basic material for
C++.
• If you are struggling with the module then concentrate on
the material I've already covered: Weeks 1 - 5.
• Go to Laurent’s and Oliver’s sessions!
• The rest of the module includes practice. Treat the rest of
the module as practice material. You do not need it to
complete the assignment to a first.
• The rest of the module is important:
• Important bits of C++.
• Leads directly into Advanced C++ next year.
• Can be used in the assignment.
2 Programming
Topic 1
The STL
3 Programming
The Standard Template Library (STL)
• Consider an array
• We can add an item to the array
• We can remove an item from the array
• We can move through each element of the array
• We use an index (for example a loop and an index
to go through each element of the array)
• We can look at the values stored in the array.
The STL
Containers
(using the vector class)
17 Programming
STL
18 Programming
Vector
19 Programming
Vector
#include <vector>
using namespace std;
int main()
{
vector <int> v(15); // vector of ints
vector <float> f(5);
// vector of floats
}
20 Programming
Vector
• Can create a vector with a size, the same way that you
would with an array:
21 Programming
Referencing vector elements
22 Programming
Vector boundaries
23 Programming
Vector boundaries
24 Programming
Vector: size
25 Programming
Vector: loop using size
27 Programming
Vector: empty
28 Programming
Vector: at
• clear
• erase
• insert
The vector class: dynamic
v.push_back( 10 );
v.push_front( 25);
vectors and functions
int main()
{
vector <int> v( 3 );
v[0] = 2;
v[1] = 5;
v[2] = 7;
DisplayVector( v );
}
Topic 3
Iterators
38 Programming
Iterators
vector <int> v( 10 );
vector <int>::iterator it;