UMBC CMSC 202 Computer Science II

Lab13: Standard Template Library


In this lab you will practice using iterators with the vector and list classes from the Standard Template Library. You will use iterators to sort, insert elements, remove elements in vectors using Standard Template Library.

Understanding Iterators

To do this lab, you will need to have a basic understanding of iterators. Here are two sources:

In this lab, we will use iterators to work with vectors and lists from the Standard Template Library (STL). Iterators do many things, we will only look at the simple uses. For example, if we had a vector V of 10 int items, we can step through the vector using a for loop: for (i = 0 ; i < V.size() ; i++) { cout << V[i] << " " ; } Using iterators we would, write: for ( it = V.begin() ; it != V.end() ; it++ { cout << *it << " " ; } The reason we might want to abandon our familiar for loop index using an int variable is that the syntax for looping through other STL data structures (e.g., a list) is exactly the same when you use an iterator.

To understand the syntax of the second for loop above, note that:

Step 0: Get the file

You will only need one file: lab13.cpp.

This C++ program has defined:

vector<int> V ; list<int> L ; vector<int>::iterator it1 ; list<int>::iterator it2 ; Some numbers have been added to V and L for you.

Step 1: Warm up

A for loop that prints out the vector V has been written. Add a for loop that prints out the linked list L using iterators.

Expected output: 60 20 40 30 70 90 60 80 10

Step 2: use the insert() function

Add code to insert the number 50 to the vector V the insert() method in vector. We want 50 to appear just before 70 in the vector. The syntax for calling insert is: V.insert(it1,50) ; but you have to set it1 to the right position.

Hint: use a while loop to loop through V until you find the 70.

Expected output: 60 20 40 30 50 70 90 60 80 10

The code for inserting into a linked list would be identical. So, we won't insert in the linked list. However, note that insert is actually more efficient in a linked list, since you don't have to shift the contents of a linked list for insertion.

Step 3: use the erase() function

Add code to remove the number 90 from the linked list L. The syntax for calling erase is: L.erase(it2) ;.

Expected output: 60 20 40 30 70 60 80 10

Step 4: use the for_each function

The for_each function applies a given function to items in the range of iterators given. The syntax is: for_each(first, last, func) ;

Here first and last are iterators. The parameter func is a function with no return value and takes exactly one parameter of the correct type by reference.

A function add5() has been written for you. Use the for_each() function to add 5 to each element of the vector V and each element of the linked list L. (You should make two class to for_each.)

Expected output:
     65 25 45 35 55 75 95 65 85 15
     65 25 45 35 75 65 85 15

Step 5: vector has random access iterators

Not all iterators are created equal. The iterator for the vector class is a "random access" iterator and has the + operator defined. Use the for_each() function and the + operator for vector iterators to apply add5 to the first 5 items in vector V.

Expected output: 70 30 50 40 60 75 95 65 85 15

Note: this would not work for the linked list L since operator+ is not defined.

Step 6: sorting

STL has a sort() function. The syntax for calling sort is: sort(first, last). Here first and last are iterators.

Expected output: 15 30 40 50 60 65 70 75 85 95

Step 7: cannot sort linked lists

Try using the same sort() function on a linked list.

Expected output: lots of compile-time errors.