 |
CoSc2030: Computer Science II
Fall Semester 2007
Instructor: Dr. Thomas Bailey
|
Lab 5: Practical Introduction to Standard Template Library (STL) Vectors and Iterators
The Standard Template Library (STL) is a collection of C++ code from a compiler vendor that makes every day programming tasks easier to complete, and greatly improves the quality of the code that we create. In this lab we take a close look at how we can use the vector in our own
programs, and will take a glimpse into one very useful container sorting routine provided by STL. In addition, we will use a new object called an iterator, and we will see how the idea of a pointer can be "supercharged" to allow for a powerful and useful extension, especially in context of STL.
To get started get the following source file: VectorDriver.cpp. There is a number of new concepts and ideas that are implemented in this file, so your TA will take a few minutes to call your attention to the new features, and you should ask lots of questions to ensure that this new material starts to make some sense before you start working on this lab.
Lab Assignment
- The function fill_vector assumes that its argument is a vector that already has allocated some space which needs to be filled. We can do this by using the push_back method provided by the vector class. Look up the push_back routine in the .NET help database and ask your TA for help if you want more clarification. Now implement the void add_numbers(vector<short> &data) method that adds 10 numbers to the data vector (feel free to use a for loop). Test your code by creating an empty vector in main(), call the add_number function, and then print the vector from main(). Another interesting test you should try is calling add_number twice in a row with the same argument - what happens?
- Take a look at the sample print function provided in the VectorDriver, and then create a new function void print_even(const vector<short>& data) that prints all of the elements in the data vector that are stored at an even index (i.e. data.at(0), data.at(2), ...) Never hesitate to ask the TA for help if you run into problems!
- In the tasks above, we used the at() method to access elements of a vector. There is another way to do it - through the use of an object called an iterator. For now, think of iterators as very sophisticated pointers that can be dereferenced using the * operator, and can be incremented via ++ (plus, plus) and decremented via -- (minus, minus). What we mean by "incrementing" an iterator is that it moves "forward" in a sequence of some items. For example, if you start with an iterator that points to the first element in the vector and then increment the iterator, the iterator will now point to the second element in the vector. There are variations and many exceptions to this basic description (as you might expect), but it should be sufficient to start off our investigation of the iterators. Take a look at the compute_sum function in the VectorDriver for an example of how one might use an iterator. Again, ask questions if things aren't clear. Then implement the bool is_present(const vector<short> &data, short value) routine that returns true if the value is present in the data vector, and false otherwise.
- You might be wondering why use the iterators if we can just use the at function. Here is one possible example that may show the usefulness of the iterator objects: the STL provides utilities besides data structures - namely useful algorithms that have been implemented for us (hooray!). One of these algorithms is the sort algorithm that is able to efficiently sort a container for us in ascending order. All we need to do is tell the sort routine where the start and end of the container are. Guess how we can do that? - Yes, by using iterators. Look up the help files on the sort routine. It is defined in the algorithm header file, so you want to include this file into your VectorDriver.cpp. Now in main, manually create a small vector of about 5 numbers in random order, and call the sort routine on the begin() and end() iterators of your vector. Did it work? What is a very simple test that you can run?
Turn In:
Upload the modified and commented Lab5VectorDriver.cpp.
Incorrectly named files will lose three points. Please make sure you
rename the file.
<< CoSc2030 Home
Copyright © 2005 Thomas Bailey